【发布时间】:2016-09-15 17:36:37
【问题描述】:
我正在尝试自定义约束
菜单栏类:
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 更新我的代码