ios - 在 Storyboard本身中将自定义类添加到 UILabel 时如何查看更改?

标签 ios swift

我已将自定义类添加到 UILabel。 自定义类是:

@IBDesignable class CustomLabel: UILabel {

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    self.setup()

}

override init(frame: CGRect) {
    super.init(frame: frame)
    self.setup()
}

override func prepareForInterfaceBuilder() {
    super.prepareForInterfaceBuilder()
    setup()
}

private func setup() {
    self.textColor = UIColor.blue
}
}

但我看不到 Storyboard的变化。它如何能够看到界面构建器中的变化??

最佳答案

您必须添加 IBInspectable 属性才能查看 Storyboard中的更改

这里是例子

@IBDesignable class RoundedTextField: UITextField {


    @IBInspectable var cornerRadius:CGFloat = 0 // You will see this in storyboard 
    @IBInspectable var borderColor:UIColor = .green  // You will see this in storyboard 

    override func awakeFromNib() {
          super.awakeFromNib()
          self.borderStyle = .none
          self.layer.cornerRadius = cornerRadius
          self.layer.borderColor = borderColor.cgColor
          self.layer.borderWidth = 2.0
          self.backgroundColor = UIColor.white
          self.layer.masksToBounds = true
      }
}

不要忘记设置类

enter image description here

这里是你如何在 Storyboard中看到

enter image description here

关于ios - 在 Storyboard本身中将自定义类添加到 UILabel 时如何查看更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54603708/

相关文章:

ios - NSHourCalendarUnit 和 NSMinuteCalendarUnit 已弃用。如何拆分小时和分钟?

ios - 如何将步进值传递给 ViewController?

ios - 转到类(class)时mp3播放失败

ios - 如何让UIView停止接收触摸事件?

ios - Swift SpriteKit 类结构的良好实践

ios - 如何在自定义 UIView 的单独文件中向 UIViewController 发送消息

ios - 清除 drawRect shape-layer 绘图中的绘图

ios - 在应用程序内快速强制暗/亮模式

iOS 11 自定义导航栏中的搜索栏

ios - 检测 iOS 中的慢速网络