UIScrollView 不使用自动布局约束 [英] UIScrollView doesn't use autolayout constraints

查看:24
本文介绍了UIScrollView 不使用自动布局约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在它后面有一个滚动视图和一个图像视图,我正在用笔尖填充它.我正在使用自动布局.我在两个视图上都有一个用于超级视图的底部空间和一个用于超级视图的顶部空间.图像视图正是我想要它做的.对于 iphone 5,这是我想要的地方.对于其他 iphone,它保持在屏幕底部上方,因此可以正确调整大小.滚动视图在 iphone 5 上看起来很正确,但在其他手机上它没有调整大小,所以它向下滚动到应用程序的视图下方.我在日志中收到这些消息:

I have a scroll view and an image view behind it and I am populating it with nibs. I am using autolayout. I have a bottom space to superview and a top space to superview on both of the views. The image view does exactly what I want it to do. For iphone 5 it is where I want it. And for the other iphones, it stays above the bottom of the screen, so it resizes correctly. The scroll view looks right on the iphone 5, but on the other phones it doesn't get resized, so it scrolls down below the view of the app. I get these messages in the log:

 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];

据我所知,这只是消除了视图中的所有约束.这不是我想要的.

From what I can see this just takes off all constraints from the views. And isn't what I want.

推荐答案

UIScrollView 和自动布局的关系不同于自动布局的其他方面.基本上,如果允许简单的自动布局操作,则不会滚动.例如,如果滚动视图的子视图以正常方式固定在距离滚动视图顶部 10 点的约束,则它绝对固定在那里;无论滚动视图如何滚动,它都不会移动.

The relationship between UIScrollView and auto layout is different from other aspects of auto layout. Basically, if simple auto layout were allowed to operate, nothing would scroll. For example, if a subview of the scroll view were pinned in the normal way by a constraint to 10 points from the top of the scroll view, it would be absolutely pinned there; it would never move, no matter how the scroll view were scrolled.

为了解决这个问题,一个使用自动布局的 UIScrollView 以一种全新的方式运行.因此,当您说我正在使用自动布局"时,您必须准备好让操作与以前大不相同.您必须使用带有 translatesAutoresizingMaskIntoConstraints = YES 和显式内容大小的单个滚动视图子视图,否则所有内容都必须具有 translatesAutoresizingMaskIntoConstraints = NO 并且内容大小将被隐式推导出基于子视图的约束.

To solve this problem, a UIScrollView that uses autolayout operates in a completely new way. Therefore when you say "I am using autolayout" you must prepare for things to operate very differently from before. You must either use a single scroll view subview with translatesAutoresizingMaskIntoConstraints = YES, and an explicit content size, or else everything must have translatesAutoresizingMaskIntoConstraints = NO and the content size will be deduced implicitly based on the constraints of the subviews.

这在 https 中有很好的解释://developer.apple.com/library/content/releasenotes/General/RN-iOSSDK-6_0/index.html

这篇关于UIScrollView 不使用自动布局约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆