IBInspectable不在故事板中更新但在模拟器上工作

时间:2018-02-28 06:04:49

标签: ios swift ibdesignable

我可以在我的模拟器上查看使用IBInspectable进行的更改,但不能在我的故事板中查看

我尝试重新安装我的Xcode,但它没有解决这个问题。以下是一些截图:

Storyboard

Simulator

所以我知道我的代码工作正常,因为它在模拟器中运行,这是一个新项目,所以我没有任何“故事板可能仍在加载更改”的问题。

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
    }
}
}

5 个答案:

答案 0 :(得分:3)

首先,您需要@IBDesignable指令在Storyboard中呈现更新,而@IBInspectable只是直接在Storyboard中访问该属性。

其次,您已扩展UIView类以在Storyboard中显示可检查的属性,但没有@IBDesignable

现在你会认为添加@IBDesignable可以解决你的问题,但遗憾的是你无法在{@ 1}这样的扩展上应用@IBDesignable

@IBDesignable //won't work on an extension
extension UIView {
    //...
}

您只能对您有权访问的类应用@IBDesignable指令,如下所示:

@IBDesignable
class MyPrettyDesignableView: UIView {
    //...
}

解决方案:

底线是你应该继承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
        }
    }

}

然后在故事板中,转到您想要在故事板中设计的视图,并将其自定义类更改为MyPrettyDesignableView

  1. 将其自定义类更改为IBDesignable子类: IBDesignable

  2. 设置IBInspectable属性: IBInspectable

答案 1 :(得分:0)

您是否在Interface Builder(编辑菜单)中检查过您已设置Automatically Refresh ViewsRefresh All Views

更新:试试这个:

 @IBDesignable public class CustomView: UIView {

 @IBInspectable var borderColor: UIColor = .white{
    didSet {
        layer.borderColor = borderColor.cgColor
    }
 }

 @IBInspectable var borderWidth: CGFloat = 2.0 {
    didSet {
        layer.borderWidth = borderWidth
    }
 }

 @IBInspectable var cornerRadius: CGFloat = 10.0 {
    didSet {
        layer.cornerRadius = cornerRadius
    }
 }

 @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
    }

  }

override public func layoutSubviews() {
    super.layoutSubviews()
    clipsToBounds = true
   }
 }

答案 2 :(得分:0)

请更新您的代码,如下所示。您必须设置layer.maskToBound = falseview.clipToBounds = false

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
        layer.maskToBounds = false
    }
}
}

答案 3 :(得分:0)

您是否尝试过检查自动刷新视图刷新所有视图?它可以在我的Xcode 9.4.1中使用。

Editor -> Automatically Refresh Views

答案 4 :(得分:0)

假设您已经在类的声明中正确应用了 @IBDesignable。假设您已经在类的属性上应用了 @IBInspectable,那么这就是对我有用的技巧。

我删除并重新输入 @IBDesignable