【问题标题】:Show bottom border of UITextField in Swift not working with Constraints.在 Swift 中显示 UITextField 的底部边框不适用于约束。
【发布时间】:2016-10-17 22:57:23
【问题描述】:

仅当我不使用任何约束时,我的显示底部边框的代码才有效。一旦我这样做,底线就消失了。有什么原因或解决方案可以解决这个问题吗?这是我当前的代码。我已经看到了很多解决方案的答案,但是当我添加约束时没有一个有效。

 func useUnderline() {

    var bottomLine = CALayer()

    bottomLine.frame = CGRect(x: 0.0, y: self.frame.height - 1, width: self.frame.width, height: 1)
    bottomLine.backgroundColor = UIColor.white.cgColor
    self.borderStyle = UITextBorderStyle.none
    self.layer.addSublayer(bottomLine)

}

【问题讨论】:

    标签: ios swift uitextfield constraints ios-autolayout


    【解决方案1】:

    任何原因

    当然是有原因的。约束会调整您的视图大小 (self)。但他们不调整图层的大小或重新定位。所以底部边框现在在错误的位置,可能在视图之外。您的代码依赖于self.frame,但约束更改 self.frame。

    当我添加约束时没有任何工作

    嗯,很简单。您需要在您的视图中实现layoutSublayersOfLayer,并在那里布置边框层,以便它保持在您想要的位置。

    【讨论】:

      【解决方案2】:

      我刚刚找到了一篇关于 objc C 的旧帖子。 您只需将视图放入 viewDidLayoutSubviews() 中,一切正常。

      【讨论】:

        猜你喜欢
        • 2015-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-10
        • 1970-01-01
        • 1970-01-01
        • 2017-05-15
        • 1970-01-01
        相关资源
        最近更新 更多