1
我有以下幾點:不能設計我@IBDesignable視圖的子視圖
@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,並且每個CustomView可能具有不同的顏色。我想在界面構建器中而不是在代碼中執行此操作。有沒有辦法通過界面編輯器來編輯我的@IBDesignable的子視圖?
您必須爲每個要更改的屬性創建IBInspectable屬性。 UIViews已經在IB中擁有了這一點,儘管你不需要創建自己的。 – soulshined
我可以在界面生成器中爲我的CustomView更改所有正常的UIView屬性,但是我無法在界面生成器中選擇我的flagView並編輯其視圖屬性。我試過'@IBInspectable var flag:UIView?'和'@IBDesignable var flag:UIView?',但都沒有成功。 – user2320861
您的子類化UIView,爲了設置故事板中的UIView,您將該類命名爲'CustomView'。但是你不需要添加IB屬性,因爲它是UIView的一個子類,它已經繼承了它。我的猜測是你沒有添加一個UIView到你的故事板並將其繼承到CustomView類 – soulshined