Creating a textfield with only bottom Line in Ios

5 Solutions Collect From Internet About “Creating a textfield with only bottom Line in Ios”

Yes it is possible. Here is how to do it:

CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor darkGrayColor].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;

Hope this helps!!

Updated for Swift 3.0

extension UITextField {    

    func useUnderline() {
        let border = CALayer()
        let borderWidth = CGFloat(1.0)
        border.borderColor = UIColor.lightGray.cgColor
        border.frame = CGRect(origin: CGPoint(x: 0,y :self.frame.size.height - borderWidth), size: CGSize(width: self.frame.size.width, height: self.frame.size.height))
        border.borderWidth = borderWidth
        self.layer.addSublayer(border)
        self.layer.masksToBounds = true
    }
}

Swift:

extension UITextField {

    func useUnderline() {

        let border = CALayer()
        let borderWidth = CGFloat(1.0)
        border.borderColor = UIColor.blackColor().CGColor
        border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, self.frame.size.height)
        border.borderWidth = borderWidth
        self.layer.addSublayer(border)
        self.layer.masksToBounds = true
    }
}

Call it like this:

yourtextFieldName.useUnderline()

Simply use CALayer to textfield with only bottom Line in Ios

CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, self.passwordField.frame.size.height - 1, self.passwordField.frame.size.width, 1.0f);
bottomBorder.backgroundColor = [UIColor blackColor].CGColor;
[self.passwordField.layer addSublayer:bottomBorder];

User4645956’s solution in Swift

private func addBottomLineToTextField(textField : UITextField) {
    let border = CALayer()
    let borderWidth = CGFloat(1.0)
    border.borderColor = UIColor.whiteColor().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 = true
}

Usage:

    self.addBottomLineToTextField(usernameTextField)
    self.addBottomLineToTextField(passwordTextField)