2

我有一个UIView就是@IBDesignable

@IBDesignable
class MyView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        sharedInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        sharedInit()
    }

    private func sharedInit(){
        translatesAutoresizingMaskIntoConstraints = false
        backgroundColor = .blue
    }

}

当我将这个 aUIView放在 Storyboard 中,并将它的类分配给MyViewIdentity inspector时,它UIView仍然有一个默认的背景颜色。为什么它的背景颜色不在UIColor.blue情节提要中?而且,请问我怎样才能做到这一点?

谢谢你的帮助。

4

3 回答 3

13

从情节提要中初始化此视图的初始化程序将在运行时调用,为了在编译时更新情节提要中的视图,您应该尝试在编译时包含prepareForInterfaceBuilder哪些更新情节提要 xib 文件。

我建议您在创建 @IBDesignable 类时做多件事:

  1. @IBDesignable标签标记类
  2. 用标记UIView属性@IBInspectable,然后您将能够使用StoryBoard更改此属性的值
  3. 在该属性获取值之前或在属性接收到值之后设置该属性的配置代码,willSet以观察更改。didSet
  4. 做您的附加设置,prepareForInterfaceBuilder()其中覆盖其超类类型UIView

简单易行!

您的代码应如下所示:

斯威夫特 5:

import UIKit

@IBDesignable
class myView: UIView {

  @IBInspectable var storyBoardColor : UIColor = .red {         
    willSet(myVariableNameToCatch) {
      self.backgroundColor = myVariableNameToCatch
    }
   }

  fileprivate func sharedInit(){
    translatesAutoresizingMaskIntoConstraints = false
    backgroundColor = storyBoardColor
  }

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

myView 的 storyBoardColor 初始值为 red,您可以从 storyBoard 更改它;)

在此处输入图像描述

于 2018-07-24T11:53:23.207 回答
1

一旦您使用标签@IBDesignable 制作视图。接下来是使用@IBInspectable 设置您的属性。

@IBDesignable
class MyView: UIView {

    @IBInspectable var myBackgroundColour: UIColor? = nil {
        willSet(v) {
            self.backgroundColor = v
        }
    }
   // YOUR EXISTING CODE HERE
}

现在,当您在Identity Inspector中将 MyView 设置为类名时。您将能够在Attributes Inspector中看到您的可检查属性。在那里您可以设置颜色,它会立即反映到您的自定义视图中。

我认为使用自定义属性设置背景颜色没有任何用处,因为 UIView 具有设置背景颜色的默认属性。

希望能帮助到你。

于 2018-07-24T11:52:55.587 回答
1

Swift 4.2 经过测试并且可以正常工作。这是最干净的解决方案。

在 Xcode 中确保Editor -> Automatically refresh views选中。

import UIKit

@IBDesignable
class BoxView: UIView {

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        layer.borderColor = borderColor?.cgColor
        layer.borderWidth = borderWidth
        layer.cornerRadius = cornerRadius
    }

    @IBInspectable var borderColor: UIColor? {
        didSet {
            layer.borderColor = borderColor?.cgColor
            setNeedsLayout()
        }
    }

    @IBInspectable var borderWidth: CGFloat = 0.0 {
        didSet {
            layer.borderWidth = borderWidth
            setNeedsLayout()
        }
    }

    @IBInspectable var cornerRadius: CGFloat = 0.0 {
        didSet {
            layer.cornerRadius = cornerRadius
            setNeedsLayout()
        }
    }
}
  1. 创建上面的 BoxView.swift 文件。
  2. 在 InterfaceBuilder 中选择一个 UIView。
  3. Identity Inspector选择Custom Class -> Class to "BoxView".
  4. Attributes Inspector集合borderColor,borderWidthborderRadius.
于 2018-12-21T20:13:50.120 回答