【问题标题】:Not getting the border at the bottom fit to textfield width底部的边框不适合文本字段的宽度
【发布时间】:2017-11-18 00:15:30
【问题描述】:

下面是 UITextField 底部边框的图片。 但没有完全强调 UITextField。

下面是我正在使用的代码。 扩展 UITextField {

    func underlined(){
        let border = CALayer()
        let width = CGFloat(1.0)
        border.borderColor = UIColor.red.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
    }

}

// 调用

yourTextField.underlined()

【问题讨论】:

  • 你在 viewdidload 中调用了 yourTextField.underlined() 或者其他
  • 是的,在 viewdidload() 函数中
  • 试着把它放在viewdidappear中

标签: ios swift swift3 uitextfield border


【解决方案1】:

你是在 UI 更新之前被调用的方法,所以同步调用你的方法主队列,否则将你的方法移动到 ViewdidAppear 或 ViewWillappear

 override func viewDidLoad() {
    super.viewDidLoad()  
DispatchQueue.main.sync {
        yourTextField.underlined()
}
}

或像这样打电话

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
     yourTextField.underlined()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-18
    • 2021-06-10
    • 2019-10-25
    • 2016-09-24
    • 2018-02-08
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多