【发布时间】:2018-06-05 15:56:44
【问题描述】:
我有一个 UIView,我正在使用 viewDidLoad 中的以下代码以编程方式添加它
dropMenuView = YNDropDownMenu(frame: CGRect(x: 0.0, y: 0, width: UIScreen.main.bounds.width, height: 40.0), dropDownViews: [typeView, brandView, priceView], dropDownViewTitles:["Type", "Brand", "Price"])
self.view.addSubview(dropMenuView)
上面的代码为当前方向创建了一个具有正确宽度的视图,但是如果用户改变他们的设备方向,视图的宽度不会更新。我尝试在 viewWillTransitionTo 中添加新约束:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
let widthConstraint = NSLayoutConstraint(item: dropMenuView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 100)
dropMenuView.addConstraints([widthConstraint])
dropMenuView.layoutIfNeeded()
}
我在上面的代码上尝试了许多突变,试图让它工作。通常我得到的错误是: 'NSLayoutConstraint for >: 未知布局属性'
我不禁认为我以错误的方式处理这件事。在方向上添加新的约束是否会改变要走的路?添加子视图后,我尝试仅添加等宽约束,但出现相同的错误。我以前从未真正以编程方式添加约束,所以这些对我来说是未知的水域。这里最好的方法是什么?
【问题讨论】:
-
你想用这个约束做什么?将宽度设置为 100?设置宽度为 view.width + 100?
标签: ios swift nslayoutconstraint screen-orientation device-orientation