<分区>
我正在通过 UIView
扩展为 UITextField
设置一些边框,如下所示。
extension UIView {
func border(side: BorderSide = .all, color:UIColor = UIColor.black, borderWidth:CGFloat = 1.0) {
let border = CALayer()
border.borderColor = color.cgColor
border.borderWidth = borderWidth
switch side {
case .all:
self.layer.borderWidth = borderWidth
self.layer.borderColor = color.cgColor
case .top:
border.frame = CGRect(x: 0, y: 0, width:self.frame.size.width ,height: borderWidth)
case .bottom:
border.frame = CGRect(x: 0, y: self.frame.size.height - borderWidth, width:self.frame.size.width ,height: borderWidth)
case .left:
border.frame = CGRect(x: 0, y: 0, width: borderWidth, height: self.frame.size.height)
case .right:
border.frame = CGRect(x: self.frame.size.width - borderWidth, y: 0, width: borderWidth, height: self.frame.size.height)
case .cusomRight:
border.frame = CGRect(x: self.frame.size.width - borderWidth - 8, y: 8, width: borderWidth, height: self.frame.size.height - 16)
}
if side.rawValue != 0 {
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
}
并通过这条线生效。
let color = UIColor.hexStringToUIColor(hex: "#1F271B")
txtLongitude.border(side: .bottom, color: color, borderWidth: 1)
而另一方面,我将 UIView
作为 UITextView
的边框。并从 InterfaceBuilder 提供背景颜色。如下图所示。
但是当我运行应用程序时。我通过 InterfaceBuilder
获得了不同的颜色。
这是屏幕截图。
我可以通过编码设置两种颜色,但我想知道为什么会这样,而我正在从 InterfaceBuilder 设置颜色?