Ash*_*nia 4 ios swift ibdesignable
我可以在模拟器上查看使用IBInspectable所做的更改,但不能在情节提要中查看
我尝试重新安装Xcode,但没有解决此问题。这是一些屏幕截图:


因此,我知道我的代码运行正常,因为它在模拟器中运行,并且这是一个新项目,因此我没有任何“ Storyboard可能仍在加载更改”的问题。
import UIKit
extension UIView {
@IBInspectable
var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
@IBInspectable
var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
@IBInspectable
var borderColor: UIColor? {
get {
let color = UIColor(cgColor: layer.borderColor!)
return color
}
set {
layer.borderColor = newValue?.cgColor
}
}
@IBInspectable
var shadowRadius: CGFloat {
get {
return layer.shadowRadius
}
set {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 0, height: 2)
layer.shadowOpacity = 0.4
layer.shadowRadius = shadowRadius
}
}
}
Run Code Online (Sandbox Code Playgroud)
首先,您需要@IBDesignable指令以在情节提要中呈现更新,而@IBInspectable仅是直接在情节提要中访问属性。
其次,您扩展了UIView该类以在Storyboard中显示可检查的属性,但没有显示@IBDesignable。
现在,您认为添加@IBDesignable可以解决您的问题,但可悲的是您不能@IBDesignable像这样申请扩展:
@IBDesignable //won't work on an extension
extension UIView {
//...
}
Run Code Online (Sandbox Code Playgroud)
您只能将@IBDesignable指令应用于您有权访问的类,如下所示:
@IBDesignable
class MyPrettyDesignableView: UIView {
//...
}
Run Code Online (Sandbox Code Playgroud)
最重要的是,您应该继承该类UIView并将@IBDesignable指令应用于该类。
基本上,您唯一的选择是:
@IBDesignable
class MyPrettyDesignableView: UIView {
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = cornerRadius
self.layer.masksToBounds = true
}
}
@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
self.layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1) {
didSet {
self.layer.borderColor = borderColor.cgColor
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在情节提要中,转到要在情节提要中设计的视图,并将其自定义类更改为MyPrettyDesignableView。
| 归档时间: |
|
| 查看次数: |
4106 次 |
| 最近记录: |