只有在不使用任何约束的情况下,我显示底部边框的代码才有效。一旦我这样做,底线就消失了。这个问题有什么原因或解决办法吗?这是我当前的代码。我已经看到了很多解决方案的答案,但是当我添加约束时,没有一个起作用。
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)
}
最佳答案
Any reasons
当然是有原因的。约束会调整您的 View 大小 (self
)。但它们不会调整层的大小或位置。所以底部边框现在在错误的位置,可能在 View 之外。您的代码依赖于 self.frame
,但约束 更改 self.frame
。
none are working when I add constraints
嗯,这很简单。您需要在您的 View 中实现 layoutSublayersOfLayer
,并在那里布置边框层,使其保持在您想要的位置。
关于ios - 在 Swift 中显示 UITextField 的底部边框不适用于约束。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40096933/