【发布时间】:2017-04-27 19:51:14
【问题描述】:
我有一个问题,我该如何解决带视图的横向模式下的约束问题?
我为在视图控制器底部创建的视图设置了约束高度 = 80 但是当设备进入横向模式时,我需要将此视图的高度约束设置为 40 ,但在这种情况下,我对约束存在问题(可能以下列表中的至少一个约束是一个你不想要。 试试这个: (1)查看每个约束并尝试找出您不期望的; (2) 找到添加了一个或多个不需要的约束的代码并修复它。 (
"<NSLayoutConstraint:0x7d175100 UIView:0x7d5eeef0.height == 40 (active)>",
"<NSLayoutConstraint:0x7d5f8ef0 UIView:0x7d5eeef0.height == 80 (active)>"
)
)
以下是我的代码:
var toolBarView:UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 80))
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor(red: 23 / 255.0, green: 120 / 255.0, blue: 104 / 255.0, alpha: 1.0)
return view
}()
func setupToolBarViewInPortrait() {
toolBarView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
toolBarView.rightAnchor.constraint(equalTo:view.rightAnchor).isActive = true
toolBarView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
toolBarView.heightAnchor.constraint(equalToConstant: 80).isActive = true
}
func setupToolBarViewInLandScape() {
toolBarView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
toolBarView.rightAnchor.constraint(equalTo:view.rightAnchor).isActive = true
toolBarView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
toolBarView.heightAnchor.constraint(equalToConstant: 40).isActive = true
}
override func viewWillLayoutSubviews() {
let oriantation = UIDevice.current.orientation
if oriantation.isLandscape {
setupNavigationBarWithObjectsInLandScape()
setupToolBarViewInLandScape()
}else if oriantation.isPortrait {
setupToolBarViewInPortrait()
}
}
【问题讨论】:
标签: ios swift uiview autolayout constraints