我試圖製作一個視圖,它將充當一種「面板」,連接到視圖控制器的右側。以編程方式創建綁定到視圖控制器邊界的約束
也就是說,它必然尾隨,頂部,父視圖控制器的底部邊緣,與300
但是固定寬度,我似乎無法得到它的權利,我無論是打破約束還是做一些事情xcode告訴我是非法的。
我在做什麼錯?
這裏是控制器
let myView = UIView()
view.backgroundColor = UIColor.redColor()
self.view.addSubview(view)
let topConstraint = NSLayoutConstraint(item: myView,
attribute: .Top,
relatedBy: .Equal,
toItem: self.topLayoutGuide,
attribute: .Bottom,
multiplier: 1,
constant: 0)
let trailingConstraint = NSLayoutConstraint(item: self.view,
attribute: .TrailingMargin,
relatedBy: .Equal,
toItem: myView,
attribute: .Trailing,
multiplier: 1,
constant: 0)
let bottomConstraint = NSLayoutConstraint(item: self.bottomLayoutGuide,
attribute: .Top,
relatedBy: .Equal,
toItem: myView,
attribute: .Bottom,
multiplier: 1,
constant: 0)
let widthConstraint = NSLayoutConstraint(item: myView,
attribute: .Width,
relatedBy: .Equal,
toItem: nil,
attribute: .NotAnAttribute,
multiplier: 1,
constant: 300)
self.view.addConstraints([trailingConstraint])
view.addConstraints([topConstraint, bottomConstraint, widthConstraint])
爲什麼你不使用Visual Format Language? ''H:[panel(300)] |'和'「V:| [toplayout] [panel] [botlayout] |」'應該足夠了 – Eendje