【问题标题】:Getting different color while setting from InterfaceBuilder and through code [duplicate]从InterfaceBuilder和通过代码设置时获得不同的颜色[重复]
【发布时间】:2017-03-20 06:27:04
【问题描述】:

我正在通过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 设置颜色?

【问题讨论】:

  • 我认为它的颜色正确,如果您想签入,请将背景颜色更改为白色并检查一次

标签: ios swift swift3 interface-builder


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-24
    • 2019-06-23
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多