我在UIScrollView
及其子视图上设置了自动布局约束时遇到了相当恼人的问题。当设备方向改变时,它会通过以下警告打破约束:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this: (1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7a1b1830 V:[UIView:0x7a1b0520(768)]>",
"<NSLayoutConstraint:0x7a1c5990 V:[UIView:0x7a1bcf50(1024)]>",
"<NSLayoutConstraint:0x7a17aa70 V:|-(0)-[UIView:0x7a1b0520] (Names: '|':UIScrollView:0x7a176520 )>",
"<NSLayoutConstraint:0x7a1c49c0 V:[UIView:0x7a1b0520]-(0)-| (Names: '|':UIScrollView:0x7a176520 )>",
"<NSLayoutConstraint:0x7a172620 V:[UIView:0x7a1bcf50]-(0)-| (Names: '|':UIScrollView:0x7a176520 )>",
"<NSLayoutConstraint:0x7a172650 V:|-(0)-[UIView:0x7a1bcf50] (Names: '|':UIScrollView:0x7a176520 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7a1c5990 V:[UIView:0x7a1bcf50(1024)]>
我在故事板中设置了一个滚动视图,它包含两个容器作为子视图。滚动视图本身固定在超视图的四个边上(编辑器/引脚/前导,后移,顶部,底部到超视图)。这两个容器的边缘固定在滚动视图上,具有自动布局约束,但宽度和宽度为1。两者的高度在故事板上设置为固定,请参阅att。图像:
两个容器的宽度和高度通过代码进行更新,最初位于viewDidLoad()
,并且每当设备方向发生变化时,都会didRotateFromInterfaceOrientation()
:
_primaryWidth.constant = view.bounds.size.width
_primaryHeight.constant = view.bounds.size.height
_secondaryWidth.constant = _primaryWidth.constant - 200
_secondaryHeight.constant = _primaryHeight.constant
这是出现上述警告并且约束中断且布局过度的情况。有人能告诉我为什么会这样,以及如何解决它?
(注意减去200.这是因为正确的容器应该有一个较小的宽度。但它不会影响问题。如果没有这个,约束就会破坏。)
答案 0 :(得分:2)
您有一些约束,可以将滚动视图的边缘固定到屏幕边缘,该边缘可以纵向和横向工作。您还有一个约束,可以将其中一个视图的高度设置为1024.
在肖像中一切正常,但是当你旋转到风景时,你不能将1024高度固定在屏幕底部的底部,因为屏幕的高度只有768。一个人必须去,你可以看到哪个人获得了斧头。
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7a1c5990 V:[UIView:0x7a1bcf50(1024)]>
删除身高限制,你会没事的。你为什么还需要它们?您希望视图固定在屏幕/超级视图的边缘,而不管您给定的大小。非任意值还允许您将相同的代码/布局带到其他设备或子视图,而无需更改布局。