我與coustom試圖限制如何解決無法同時滿足的約束
MenuBar類:
import UIKit
class ManuBar: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.blueColor()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension UIView {
func addConstraintsWithFormat(format: String, views: UIView...) {
var viewsDictionary = [String: UIView]()
for (index, view) in views.enumerate() {
let key = "v\(index)"
view.translatesAutoresizingMaskIntoConstraints = false
viewsDictionary[key] = view
}
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
}
}
視圖控制器:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupManuBar();
}
let menuBar : ManuBar = {
let mb = ManuBar()
return mb
}()
private func setupManuBar(){
view.addSubview(menuBar)
view.addConstraintsWithFormat("H:|[v0]|",views : menuBar)
view.addConstraintsWithFormat("V:|-16-[v0(40)]|",views : menuBar)
}
}
無法同時滿足約束條件。
以下列表中的至少一個約束可能是您不想要的約束之一。
試試這個: (1)看看每個約束,並試圖找出你不期望的;
(2)發現,增加了不必要的約束或
constraints and fix it.
(
"<NSLayoutConstraint:0x7ff891d27c70 V:|-(16)-[Tab_Menu_Bar_Programmatically.ManuBar:0x7ff891d19f30] (Names: '|':UIView:0x7ff891d1b570)>",
"<NSLayoutConstraint:0x7ff891d27f40 V:[Tab_Menu_Bar_Programmatically.ManuBar:0x7ff891d19f30(40)]>",
"<NSLayoutConstraint:0x7ff891d0fc50 V:[Tab_Menu_Bar_Programmatically.ManuBar:0x7ff891d19f30]-(0)-| (Names: '|':UIView:0x7ff891d1b570)>",
"<NSLayoutConstraint:0x7ff891d1c8e0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7ff891d1b570(736)]>"
)
將嘗試如果打破約束
恢復的代碼你需要更多的信息,讓我知道
請發表你的約束截圖你的問題。如果你不知道如何做到這一點,請告訴我。 –
@DanLevy更新我的代碼 –