gpt4 book ai didi

ios - layer被constraints更新后调用@IBDesignable中的@IBInspectable set

转载 作者:行者123 更新时间:2023-11-28 15:40:10 25 4
gpt4 key购买 nike

我想为 UIView 创建一个 @IBInspectable,它使 UIView 成为一个圆圈:

@IBInspectable var circleBorder: Bool {
set {
layer.cornerRadius = layer.frame.size.width / 2
}
get {
return layer.cornerRadius > 0 ? true : false
}
}

问题是我的@IBInspectable 在约束修改其值之前被调用,并且设置了错误的 cornerRadius。是否可以在这里得到约束后的图层大小?

最佳答案

我认为没有,但有更好的方法 - 公开 cornerRadius

这样开发人员就可以按自己的顺序做事。在 Bool 中公开事物意味着在 IB 中,开发人员需要 (1) 创建 View ,(2) 添加约束,(3) 更新框架,然后 (4) 设置Bool。 (此外,在 Xcode 8 中,我猜测如果您在 IB 中更改设备 - 即使在获得正确的序列之后 - 事情可能无法正确更新。

除非您有特定的原因(我无法想出)将其设为 Bool,否则这不仅无害,而且更可定制。

代码:

@IBInspectable public var cornerRadius:CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}

关于ios - layer被constraints更新后调用@IBDesignable中的@IBInspectable set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43803898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com