Add bottom line to view in SwiftUI / Swift / Objective-C / Xamarin

前端 未结 22 2331
自闭症患者
自闭症患者 2020-11-27 09:19

I would like to keep the border at the bottom part only in UITextField. But I don\'t know how we can keep it on the bottom side.

Can you please advise m

22条回答
  •  暖寄归人
    2020-11-27 09:45

    Updated Code:

    Swift 5.0

    extension UITextField {
      func addUnderline() { 
       let layer = CALayer()
       layer.backgroundColor = #colorLiteral(red: 0.6666666865, green: 0.6666666865, blue: 0.6666666865, alpha: 1)
       layer.frame = CGRect(x: 0.0, y: self.frame.size.height - 1.0, width: self.frame.size.width, height: 1.0)
       self.clipsToBounds = true
       self.layer.addSublayer(layer)
       self.setNeedsDisplay()} }
    

    Now call this func in viewDidLayoutSubviews()

    override func viewDidLayoutSubviews() {
        textField.addUnderline()
    }
    

    NOTE: This method will only work in viewDidLayoutSubviews()

提交回复
热议问题