objective-c – 在iOS 8 / Xcode 6 GM Seed上使用autolayout大小时,UIScrollView会中断

如何使用自动布局来计算其内容大小?

每当Scroll视图(带有Autolayout)存在时,Xcode 6似乎会创建一个“UIView Encapsulated Layout Height”约束,并且此约束将强制将高度设置为帧高度(从而使滚动视图的滚动功能无效).

错误

2014-09-09 21:06:01.059 ScrollViewLayoutBreaking[24488:88731] 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:0x7f9a19ebd150 V:[UIView:0x7f9a19ea7f90(530)]>","<NSLayoutConstraint:0x7f9a19ec1370 V:[UIView:0x7f9a19ebd5e0(110)]>","<NSLayoutConstraint:0x7f9a19ea9ee0 V:[_UILayoutGuide:0x7f9a19ec18d0]-(0)-[UIView:0x7f9a19ea7f90]>","<NSLayoutConstraint:0x7f9a19ec1fa0 V:[UIView:0x7f9a19ea7f90]-(0)-[UIView:0x7f9a19ebd5e0]>","<NSLayoutConstraint:0x7f9a19e8d270 V:[UIView:0x7f9a19ebd5e0]-(5)-[_UILayoutGuide:0x7f9a19ec2260]>","<_UILayoutSupportConstraint:0x7f9a19ea0ac0 V:[_UILayoutGuide:0x7f9a19ec18d0(0)]>","<_UILayoutSupportConstraint:0x7f9a19ea7380 V:|-(0)-[_UILayoutGuide:0x7f9a19ec18d0]   (Names: '|':UIScrollView:0x7f9a19ea76a0 )>","<_UILayoutSupportConstraint:0x7f9a19e21d00 V:[_UILayoutGuide:0x7f9a19ec2260(0)]>","<_UILayoutSupportConstraint:0x7f9a19e8a930 _UILayoutGuide:0x7f9a19ec2260.bottom == UIScrollView:0x7f9a19ea76a0.bottom>","<NSLayoutConstraint:0x7f9a1c003f50 'UIView-Encapsulated-Layout-Height' V:[UIScrollView:0x7f9a19ea76a0(504)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f9a19ebd150 V:[UIView:0x7f9a19ea7f90(530)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

编辑:此滚动视图中的所有视图都是自动循环的,并已固定到滚动视图的顶部和底部以及两侧.我知道未能在所有四个方面绑定它将无法创建内容大小. iOS 7 / XCode 5中的自动布局和滚动是正确的,但iOS 8 / XCode 6中完全相同的代码和故事板中断.

解决方法

通过你的截图,我猜你在滚动视图中使用自定义视图所以……

你的内容观点是:STH,Foo,Bar,Baz,Qux,Blah,Woot,Lorem,Ipsum,Dolor

代码中,您可以通过自动布局自动设置内容大小,方法是设置彼此相关的所有内容视图,但顶视图和底视图除外……这些视图必须与滚动视图的边缘相关,这是一个示例:

-(void)example{

UIView *parentView; //Can be the Scroll View or the Scroll View superview,it's better for the ScrollView to be the Owner of the restrictions
UIView *prevIoUsView;
UIScrollView *scrollView;

NSArray * arrayOfContentViews = @[STH,Dolor];

for (UIView * currentView in arrayOfContentViews) {

    //Important for Autolayout Restrictions
    currentView.translateAutoresizingMask = NO;
    [scrollview addSubview:currentView];

    if (prevIoUsView == nil) {
        //For the Top,Left and Right Attribute Edges of the ScrollView
        prevIoUsView = scrollView;

        [parentView addConstraint:[NSLayoutConstraint constraintWithItem:currentView
                                                               attribute:NSLayoutAttributetop
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:prevIoUsView
                                                               attribute:NSLayoutAttributetop
                                                              multiplier:1.0
                                                                constant:0.0]];

        //10% of the scrollview Frame Size (not Content Size)
        [parentView addConstraint:[NSLayoutConstraint constraintWithItem:currentView
                                                               attribute:NSLayoutAttributeHeight
                                                               relatedBy:NSLayoutRelationGreaterThanorEqual
                                                                  toItem:prevIoUsView
                                                               attribute:NSLayoutAttributeHeight
                                                              multiplier:0.1
                                                                constant:0.0]];            

    }else{

        [parentView addConstraint:[NSLayoutConstraint constraintWithItem:currentView
                                                               attribute:NSLayoutAttributetop
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:prevIoUsView
                                                               attribute:NSLayoutAttributeBottom
                                                              multiplier:1.0
                                                                constant:0.0]];

        [parentView addConstraint:[NSLayoutConstraint constraintWithItem:currentView
                                                               attribute:NSLayoutAttributeHeight
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:prevIoUsView
                                                               attribute:NSLayoutAttributeHeight
                                                              multiplier:1.0
                                                                constant:0.0]];
    }

    [parentView addConstraint:[NSLayoutConstraint constraintWithItem:currentView
                                                           attribute:NSLayoutAttributeLeft
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:prevIoUsView
                                                           attribute:NSLayoutAttributeLeft
                                                          multiplier:1.0
                                                            constant:0.0]];

    [parentView addConstraint:[NSLayoutConstraint constraintWithItem:currentView
                                                           attribute:NSLayoutAttributeRight
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:prevIoUsView
                                                           attribute:NSLayoutAttributeRight
                                                          multiplier:1.0
                                                            constant:0.0]];


    prevIoUsView = currentView;

    }

//Last View Dolor to the Bottom Edge of the scrollView
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:prevIoUsView
                                                       attribute:NSLayoutAttributeBottom
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:scrollView
                                                       attribute:NSLayoutAttributeBottom
                                                      multiplier:1.0
                                                        constant:0.0]];
}

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...