1

我有以下内容:

@IBDesignable class CustomView: UIView {

    @IBInspectable var hasFlag: Bool = true

    var flagView: UIView?

    override init(frame: frame) {

        super.init(frame)

        setup()

    }

    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        setup()

    }

    func setup() {

        if (hasFlag) {

            flagView = UIView()

            flagView!.translatesAutoresizingMaskIntoConstraints = false

            flagView!.backgroundColor = UIColor.redColor()


            addSubview(flagView!)

            flagView!.leadingAnchor.constraintEqualToAnchor(leadingAnchor, constant: 0).active = true

            flagView!.topAnchor.constraintEqualToAnchor(topAnchor, constant: 0).active = true

            flagView!.widthAnchor.constraintEqualToConstant(8).active = true

            flagView!.bottomAnchor.constraintEqualToAnchor(bottomAnchor, constant: 0).active = true

        }

    }

}

但是,当我进入界面生成器时,我在那里看到了我的红旗,但我没有选择 flagView 的选项,比如设置它的背景颜色。我也没有看到它出现在我的 CustomView 下的视图层次结构中。我将有多个 CustomView,每个都可能有不同的颜色。我想在界面生成器中而不是在代码中执行此操作。有没有办法可以通过界面生成器来编辑我的@IBDesignable 的子视图?

4

1 回答 1

0

您可以在 layoutSubviews 中尝试“setup()”。

override func layoutSubviews() { 
     super.layoutSubviews()
     setup()
}
于 2017-01-12T09:03:32.730 回答