How to Only Show Bottom Border of Uitextfield in Swift

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



Leave a reply



Submit