How to only show bottom border of UITextField in Swift
Try to do by this way, with Swift 5.1:
var bottomLine = CALayer()
bottomLine.frame = CGRect(x: 0.0, y: myTextField.frame.height - 1, width: myTextField.frame.width, height: 1.0)
bottomLine.backgroundColor = UIColor.white.cgColor
myTextField.borderStyle = UITextField.BorderStyle.none
myTextField.layer.addSublayer(bottomLine)
You have to set the borderStyle
property to None
If you are using the autolayout then set perfect constraint else bottomline will not appear.
Hope it helps.
UITextField Only Top And Bottom Border
One approach I have found works good is using layers. Here's a snippet:
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, self.frame.size.height - 1, self.frame.size.width, 1.0f);
bottomBorder.backgroundColor = [UIColor blackColor].CGColor;
[myTextField.layer addSublayer:bottomBorder];
Hope this helps someone.
How to leave only the lower border of UITextField?
Replace this
bottomLine.frame = CGRect(x:0.0, y:1.0, width: myTextField.frame.width,height: myTextField.frame.height - 1)
with
bottomLine.frame = CGRect(x:0.0, y:myTextField.frame.height - 1, width: myTextField.frame.width,height: 1)
Show bottom border of UITextField in Swift not working with Constraints.
Any reasons
Certainly there's a reason. The constraints resize your view (self
). But they do not resize or reposition the layer. So the bottom border is now in the wrong place, possibly outside the view. Your code depends upon self.frame
, but constraints change self.frame
.
none are working when I add constraints
Well, it's pretty simple. You need to implement layoutSublayersOfLayer
in your view, and lay out the border layer there so that it stays where you want it.
UITextField having bottom border only in Objective-C
First:
Add and import QuartzCore
framework.
#import <QuartzCore/QuartzCore.h>
Second:
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor grayColor].CGColor;
border.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField.frame.size.width, textField.frame.size.height);
border.borderWidth = borderWidth;
[textField.layer addSublayer:border];
textField.layer.masksToBounds = YES;
EDIT:
If you have more TextFields, make one common method which takes UITextField
and applies border to it like below:
-(void)SetTextFieldBorder :(UITextField *)textField{
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor grayColor].CGColor;
border.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField.frame.size.width, textField.frame.size.height);
border.borderWidth = borderWidth;
[textField.layer addSublayer:border];
textField.layer.masksToBounds = YES;
}
Pass your TextField as follows to set Bottom Border:
[self SetTextFieldBorder:YourTextField];
UITextField border-bottom will not be displayed fully
Create an extension
import Foundation
import UIKit
extension CALayer {
func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
let border = CALayer()
switch edge {
case UIRectEdge.top:
border.frame = CGRect.zero
border.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: thickness)
break
case UIRectEdge.bottom:
border.frame = CGRect(x: 0, y: self.bounds.height - thickness, width: self.bounds.width, height: thickness)
break
case UIRectEdge.left:
border.frame = CGRect(x: 0, y: 0, width: thickness, height: self.bounds.height)
break
case UIRectEdge.right:
border.frame = CGRect(x: self.bounds.width - thickness, y: 0, width: thickness, height: self.bounds.height)
break
default:
break
}
border.backgroundColor = color.cgColor;
self.addSublayer(border)
}
}
and call them from controller
someTextField.layer.addBorder(edge: UIRectEdge.bottom, color: UIColor.red, thickness: 2)
UPDATE ANSWER : Create a new function and adding width as param
func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat, width: CGFloat) {
let border = CALayer()
switch edge {
case UIRectEdge.top:
border.frame = CGRect(x: 0, y: 0, width: width, height: thickness)
break
case UIRectEdge.bottom:
border.frame = CGRect(x: 0, y: self.bounds.height - thickness, width: width, height: thickness)
break
case UIRectEdge.left:
border.frame = CGRect(x: 0, y: 0, width: thickness, height: self.bounds.height)
break
case UIRectEdge.right:
border.frame = CGRect(x: width - thickness, y: 0, width: thickness, height: self.bounds.height)
break
default:
break
}
border.backgroundColor = color.cgColor;
self.addSublayer(border)
}
After, you have to create a constraint for your TextField WIDTH
and call the extension by passing the constraint width, like this
@IBOutlet weak var textFieldConstraintWidth: NSLayoutConstraint!
self.textField.layer.addBorder(edge: UIRectEdge.bottom, color: UIColor.red, thickness: 2, width: textFieldConstraintWidth.constant)
Related Topics
Change Table to Edit Mode and Delete Rows Inside a Normal Viewcontroller
Bridge Google Drive API to Swift
Swift 3.0, Alamofire 4.0 Extra Argument 'Method' in Call
How to Use Multi-Path Update with the Rest API in Firebase? Error 400
@State Var Not Updated as Expected in Lazyvgrid
Cannot Assign Value of Type '() -> Void' to Type '(() -> Void)!'
Why My Nsdateformatter Returns Null
iOS Simulator Display Scaled Incorrectly
iOS Bar Item Image Displaying Wrong Color
How to Delete Object from Core Data in Swift 3
How to Return Value After the Execution of the Block? Swift
How to Upload Audio with Alamofire Multipart Upload
How to Get the Status Bar Height in iOS 13
Swift: Get All Subviews of a Specific Type and Add to an Array
Avaudiosession Setcategory Swift 4.2 iOS 12 - Play Sound on Silent
Can't Set Titleview in the Center of Navigation Bar Because Back Button