首页 > 解决方案 > 设备旋转更改时会创建附加约束

问题描述

我有一个子视图,其约束通过 IBOutlet 引用超级视图,我通过停用它然后赋予新值并再次激活来更改约束,这工作正常并且没有警告但是当设备旋转时会出现问题,它似乎是另一个正在添加约束(我在情节提要中添加的约束)代码:

bottomViewBottomConstraint.isActive = false
if self.isHide {
    bottomViewBottomConstraint = bottomView.bottomAnchor.constraint(equalTo: playerView.bottomAnchor,constant:0)
}
else {
    bottomViewBottomConstraint = bottomView.bottomAnchor.constraint(equalTo: playerView.bottomAnchor,constant:bottomView.frame.height + 50)
}
bottomViewBottomConstraint.isActive = true

旋转后警告:

"<NSLayoutConstraint:0x600003fdce60 UIView:0x7fc034f050b0.bottom == PlayKit.PlayerView:0x7fc034f69870.bottom + 143.333   (active)>",
"<NSLayoutConstraint:0x600003fe3480 'bottomViewId' V:[UIView:0x7fc034f050b0]-(0)-|   (active, names: 
'|':PlayKit.PlayerView:0x7fc034f69870 )>"

底视图故事板约束 在此处输入图像描述

标签: iosswiftautolayout

解决方案


你需要它一个viewDidLoad

var bottomViewBottomConstraint:NSLayoutConstraint!

bottomViewBottomConstraint = bottomView.bottomAnchor.constraint(equalTo: playerView.bottomAnchor,constant:0)

并不断地玩

bottomViewBottomConstraint.constant = self.isHide ? 0 :   bottomView.frame.height + 50
self.view.layoutIfNeeded()

推荐阅读