从InterfaceBuilder和代码设置时获得不同的颜色

时间:2017-03-20 06:27:04

标签: ios swift swift3 interface-builder

我通过UITextField分机设置了UIView的边框,如下所示。

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中提供背景颜色。如下图所示 Setting color via InterfaceBuilder

但是当我运行应用程序时。我通过InterfaceBuilder获得了不同的颜色。 这是截图 Color which is gave by code
Color which is gave by InterfaceBuilder
我可以通过编码设置两种颜色,但我想知道为什么会发生这种情况,而我是从InterfaceBuilder设置颜色的?

1 个答案:

答案 0 :(得分:3)

之前我遇到过这个问题 在下方图片中的标记按钮中,从下拉列表中选择Device RGB而不是Generic RGB 你会发现那些以编程方式制作你想要的颜色的值

enter image description here