Skip to content

Instantly share code, notes, and snippets.

@tonyedwardspz
Last active July 6, 2020 11:12
Show Gist options
  • Save tonyedwardspz/82e753fc1934a421095b to your computer and use it in GitHub Desktop.
Save tonyedwardspz/82e753fc1934a421095b to your computer and use it in GitHub Desktop.
extension UITextField {
func underlined(){
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor.lightGrayColor().CGColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
// Call with....
yourTextField.underlined()
@minwoolee
Copy link

minwoolee commented Mar 5, 2019

I read somewhere that bounds property is required. Adding this line worked for me

border.bounds = CGRect(x: 0, y: 0, width:  self.frame.size.width, height: self.frame.size.height)

@Sopharethraksmey
Copy link

I have applied this method and it seem working fine but cannot apply all screen device.
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor.lightGray.cgColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment