ios - 从 InterfaceBuilder 和通过代码设置时获得不同的颜色

标签 ios swift swift3 interface-builder

<分区>

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

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

最佳答案

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

enter image description here

关于ios - 从 InterfaceBuilder 和通过代码设置时获得不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42897084/

相关文章:

ios - 敏感信息应该存储在哪里?

swift - 滑动操作延迟

swift - 如果在Class中创建了对象,如何在B类中观察?

ios - 手机间隙 : Plugin development tutorial for iOS

ios - 在 tvOS 应用程序中使用 iOS 框架

ios - 通用深度链接问题

html - 如何在 Safari 上打开本地 html 文件?

ios - 从今日扩展(小部件)在主机应用程序中执行操作,无需打开应用程序 ios

swift - Realm 提交写入错误 - 无法提交不存在的写入事务

ios - 在 Xcode 中调整图像大小?