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 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 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];
Text Field with Only top and Bottom Border using swift 2
Thanks @El Captain for the valuable comment and nice answer by @Bjorn Ro even if it was in Objective-c i think.
And my answer for the question is (i'm using swift 2 Xcode 7)
Override the function viewDidLayoutSubviews() in your swift file. And the Code for the same is
override func viewDidLayoutSubviews() {
// Creates the bottom border
let borderBottom = CALayer()
let borderWidth = CGFloat(2.0)
borderBottom.borderColor = UIColor.grayColor().CGColor
borderBottom.frame = CGRect(x: 0, y: forgotPasswordEmailText.frame.height - 1.0, width: forgotPasswordEmailText.frame.width , height: forgotPasswordEmailText.frame.height - 1.0)
borderBottom.borderWidth = borderWidth
forgotPasswordEmailText.layer.addSublayer(borderBottom)
forgotPasswordEmailText.layer.masksToBounds = true
// Creates the Top border
let borderTop = CALayer()
borderTop.borderColor = UIColor.grayColor().CGColor
borderTop.frame = CGRect(x: 0, y: 0, width: forgotPasswordEmailText.frame.width, height: 1)
borderTop.borderWidth = borderWidth
forgotPasswordEmailText.layer.addSublayer(borderTop)
forgotPasswordEmailText.layer.masksToBounds = true
}
forgotPasswordEmailText is the text field for entering Email
The Final output looks like this... with a gray Colour border (Screen shot of iPhone 4s Simulator)
Bottom Border in UiTextfield
If you want to deal with CALayer
then Try this following life cycle method.
-(void)viewDidLayoutSubviews
{
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, self.frame.size.height - 1, self.layer.frame.size.width, 1.0f);
bottomBorder.backgroundColor = self.BorderColor.CGColor;
[self.layer addSublayer:bottomBorder];
}
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)
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
How to Convert .Dae to .Scn Files in Scenekit
Which Tasks Are More Suitable to Nsoperation Than Gcd
How to Make Sure API Requests Come from Our Mobile (Ios/Android) App
Native Facebook Login Stopped Working After Sdk Update to 3.14
How to Adjust the Height of a Textview to His Content in Swift
Custom Url Scheme for New Facebook iOS App
App Crashes Only on Testflight Build
Swift - iOS - Dates and Times in Different Format
Setting Alpha on Uiview Sets the Alpha on Its Subviews Which Should Not Happen
Linker Errors When Trying to Install New Google Analytics 3.0 Beta
How to Track User Location in Background
How to Get the Custom Overlay for Uiimagepicker Camera to Be Full Screen in iOS 7
Save an Object in Nsuserdefaults and Realm
How to Force My Keyboard to Be Up on My Program's Start in Swift