UIScrollView不使用自动布局约束

时间:2012-11-21 18:11:14

标签: objective-c uiscrollview constraints autolayout

我有一个滚动视图和一个背后的图像视图,我用笔尖填充它。我正在使用autolayout。我有一个底层空间可以看到超级视图,还有一个顶层空间可以在两个视图上进行超视图。图像视图完全符合我的要求。对于iphone 5来说,它就是我想要它的地方。而对于其他iphone,它保持在屏幕底部之上,因此它可以正确调整大小。滚动视图在iphone 5上看起来是正确的,但在其他手机上它没有调整大小,所以它向下滚动到应用程序的视图下方。我在日志中收到这些消息:

 2012-11-21 10:42:38.576 LCHApp[12604:907] 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:0x1d8ea080 UIScrollView:0x1d8413b0.bottom == UIImageView:0x1d892110.bottom>",
"<NSAutoresizingMaskLayoutConstraint:0x1d8cca10 h=-&- v=-&- ScheduleViewNib:0x1d853630.height == UIScrollView:0x1d8413b0.height - 386>",
"<NSLayoutConstraint:0x1d8e5340 V:[UIImageView:0x1d892110]-(64)-|   (Names: '|':ScheduleView:0x1d8efc30 )>",
"<NSAutoresizingMaskLayoutConstraint:0x1d8cf520 h=--& v=--& V:[ScheduleView:0x1d8efc30(480)]>",
"<NSLayoutConstraint:0x1d8eaed0 V:|-(45)-[UIScrollView:0x1d8413b0]   (Names: '|':ScheduleView:0x1d8efc30 )>"


 Will attempt to recover by breaking constraint 
 <NSLayoutConstraint:0x1d8ea080 UIScrollView:0x1d8413b0.bottom ==      UIImageView:0x1d892110.bottom>

我已经尝试了

[self setTranslatesAutoresizingMaskIntoConstraints:YES];

[self.myScrollView setTranslatesAutoresizingMaskIntoConstraints:YES];

从我所看到的,这只是从视图中取消所有约束。并不是我想要的。

4 个答案:

答案 0 :(得分:172)

UIScrollView和自动布局之间的关系与自动布局的其他方面不同。基本上,如果允许简单的自动布局操作,则不会滚动任何内容。例如,如果滚动视图的子视图以正常方式通过约束从滚动视图顶部的10个点固定,那么它将被绝对固定在那里;无论滚动视图如何滚动,它都不会移动。

要解决此问题,使用autolayout的UIScrollView以全新的方式运行。因此当你说&#34;我正在使用autolayout&#34;你必须做好准备,让事情与以前完全不同。您必须使用带有translatesAutoresizingMaskIntoConstraints = YES的单个滚动视图子视图和明确的内容大小,否则所有内容都必须translatesAutoresizingMaskIntoConstraints = NO,并且内容大小将根据子视图的约束隐式推断。

https://developer.apple.com/library/content/releasenotes/General/RN-iOSSDK-6_0/index.html

对此进行了详细解释

答案 1 :(得分:161)

使用自动布局时非常重要:您必须将最后一个子视图的右侧和/或底部固定在滚动视图的右侧和/或底部。这是滚动视图知道内容大小的方式。例如:

[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:lastSubView
                                                 attribute:NSLayoutAttributeRight
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:scrollView
                                                 attribute:NSLayoutAttributeRight
                                                multiplier:1.0
                                                  constant:0]];

感谢this site提供完美的例子。

因为这个原因我失去了几个小时,我希望能让别人感到痛苦。

答案 2 :(得分:0)

为了使UIScrollviews能够很好地处理约束,我使用这种方法回答here。在那个答案中,我解决了如何使垂直滚动滚动视图工作,也适用于设备旋转。您也可以调整使用水平滚动滚动视图的方法。对于向两个方向滚动的滚动视图,不要添加大小匹配宽度约束技巧。但是其他一切都是一样的。

答案 3 :(得分:-3)

有几件事。

  1. 确保自动布局已打开(“文件检查器”选项卡上的IB)
  2. 确保您没有进行涉及边界,框架等的任何更改 - 现在所有这些都是通过自动约束来完成的
  3. 确保远离AutoResizingMask。这将与您的新设置竞争。 如果这些操作正确,您现在可以布置您的按钮,它将工作得很好。这是怎么回事。
  4. 此错误表明您的笔尖或该笔尖内的控件未使用自动布局。

相关问题