UITextField with underline iOS swift

Solution:

If you had a textField that wants to be underlined follow the below steps you can achieve.

1. First you have to choose your textfield as custom in your storyboard.

2. Then add an extension to textfield

3. Then assign the delegate to the textfield.call your custom function and then enjoy with the underline.

Coding:


// MARK: uitextField

extension UITextField {
    func underlined(){
        let border = CALayer()
        let lineWidth = CGFloat(0.3)
        border.borderColor = UIColor.darkGray.cgColor
        border.frame = CGRect(x: 0, y: self.frame.size.height - lineWidth, width:  self.frame.size.width, height: self.frame.size.height)
        border.borderWidth = lineWidth
        self.layer.addSublayer(border)
        self.layer.masksToBounds = true
    }
}

import UIKit


class ViewController: UIViewController,UITextFieldDelegate {

    override func viewDidLoad() {
            userNameTF.delegate = self
            userNameTF.underlined()
}

}

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift