-
Add this extension to your code.
extension UIView { func addBottomBorder(color: UIColor, width: CGFloat) { let border = CALayer() border.backgroundColor = color.cgColor border.frame = CGRect(x: 0, y: frame.size.height - width, width: frame.size.width - 25, height: width) layer.addSublayer(border) } }
-
Make sure you have imported
UIKit
framework.import UIKit
-
Then inside your ViewController file call
addBottomBorder()
fromviewDidLayoutSubviews()
function as shown bellow:override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() yourTextFieldName.addBottomBorder(color: .gray, width: 0.5) }
-
Make sure your text field has no background color to have the same look. (Optional)
yourTextFieldName.backgroundColor = .clear
You can reach me on Twitter.