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

前端 未结 22 2311
自闭症患者
自闭症患者 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

    Please have a look at the below code sample;

    Swift 4:

    @IBDesignable class DesignableUITextField: UITextField {
    
        let border = CALayer()
    
        @IBInspectable var borderColor: UIColor? {
            didSet {
                setup()
            }
        }
    
        @IBInspectable var borderWidth: CGFloat = 0.5 {
            didSet {
                setup()
            }
        }
    
        func setup() {
            border.borderColor = self.borderColor?.cgColor
    
            border.borderWidth = borderWidth
            self.layer.addSublayer(border)
            self.layer.masksToBounds = true
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
            border.frame = CGRect(x: 0, y: self.frame.size.height - borderWidth, width:  self.frame.size.width, height: self.frame.size.height)
        }
     }
    

提交回复
热议问题