【问题标题】:IBInspectable run setup function every time IBInspectable value changes每次 IBInspectable 值更改时,IBInspectable 运行设置函数
【发布时间】:2018-04-18 14:31:32
【问题描述】:

我有一个 IBDesignable 类,它有一个 IBInspectable 值:Int

value 基本上是 IBDesignable 类中显示的球数

所以我有一个 setup() 函数,它使用 for _ 0 .. 添加正确的 imageViews(每个球一个)

但是在更改值时,我在 Interface Builder 中看不到新的球数

也许但 Designables:更新停留在那里......

如何让 setup() 在每次 Value 改变时执行?

提前致谢

【问题讨论】:

  • 你在模拟器中看到你的更新了吗?
  • 在设备或模拟器上运行一切正常,只是在构建视图时不在界面生成器中

标签: ios swift ibdesignable ibinspectable


【解决方案1】:

试试这个代码

@IBDesignable
class SNButton:UIButton{

    @IBInspectable var val: Int = 0

    override func layoutSubviews() {
        super.layoutSubviews()


        for i in 0...val {
         let ui = UIImageView(frame: CGRect(x: i, y: 0, width: 20, height: 20))
            ui.backgroundColor = UIColor.red
            self.addSubview(ui)
        }
      }        

    }

但我不建议你这样做,因为你会注意到 故事板变得很慢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多