简体   繁体   English

无法设计我的@IBDesignable视图的子视图

[英]Can't design subview of my @IBDesignable view

I have the following: 我有以下几点:

@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

        }

    }

}

However, when I go into interface builder, I see my red flag there but I don't have the option to select the flagView to, say, set its background color. 但是,当我进入界面生成器时,在那里看到了我的红旗,但是我没有选择flagView的选项,例如设置它的背景色。 Nor do I see it appear in the view hierarchy under my CustomView. 我也看不到它出现在CustomView下的视图层次结构中。 I'm going to have multiple CustomViews, and each one might have a different color. 我将有多个CustomView,每个视图可能具有不同的颜色。 I'd like to do this in interface builder rather than in code. 我想在界面生成器而不是代码中执行此操作。 Is there a way I can achieve editing my subview of my @IBDesignable through interface builder? 有没有一种方法可以通过界面生成器来编辑@IBDesignable的子视图?

You can try "setup()" in layoutSubviews. 您可以在layoutSubviews中尝试“ setup()”。

override func layoutSubviews() { 
     super.layoutSubviews()
     setup()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM