Scroll View内的ContentView不会增加高度

时间:2015-11-17 05:00:59

标签: ios objective-c uiview uiscrollview ios-autolayout

我为ViewController

设置了以下contrstraints

enter image description here

然后我添加这样的自定义视图。

-(void)setUI
  {
    int i;
for ( i=0; i<[array count]; i++)
 {

    NSLog(@"Object at index %i  %@",i,[array objectAtIndex:i]);

    UIView *view=[self GetAlertView:i];


    float sizeOfContent = 0;

    if (notY+view.frame.size.height>self.contentView.frame.size.height+self.contentView.frame.origin.y) {
        CGRect frame=self.contentView.frame;
        frame.size.height=notY;
        self.contentView.frame=frame;
    }


    [self.contentView addSubview:view];
    self.mainScroll.contentSize = self.contentView.frame.size;

    NSLog(@"CONTENT VIEW FRAME HEIGHT==== %f",self.contentView.frame.origin.y+self.contentView.frame.size.height);

   }


 }

但我的问题是这个内容视图不会永久增加其大小。在log cat我可以看到hight正在改变。

CONTENT VIEW FRAME HEIGHT==== 8913.000000

但它不是持久性的。在UI中我可以看到它的高度只是保持一定高度,并且只有3个子视图添加到内容视图中,而其他视图不在内容视图中。这是什么原因?请帮我 感谢

1 个答案:

答案 0 :(得分:1)

当您使用AutoLayout时,应该避免设置UIScrollView的contentSize,而是依赖于子视图的高度和底部约束来让UIScrollView在内部计算它。

其次,您使用了不需要的UIView来呈现您的子视图。您不需要ContentView来托管您的子视图。直接将这些添加到mainScroll视图。

使用此代码在滚动视图中垂直布局子视图。在这里,我采取了一些假设,例如view.width = mainScroll.widthview.width = view.height,并且视图的每个边界的填充为5px。您可以随意进行更改。
首先,在类中创建一个实例变量:

UIView *lastView;  
NSInteger arrayCount;

然后用此

替换您的setUI代码
-(void)setUI
{
    lastView = nil;
    arrayCount = [array count];

    for(NSInteger index =0; index < arrayCount; index++)
    {
        UIView *view = [self GetAlertView:i];
        [self.mainScroll addSubview:view];

        [view setTranslatesAutoresizingMaskIntoConstraints:NO];

        [self.mainScroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[view]-0-|" options:0 metrics:nil views:@{@"view":view}]];

        //--> If View is first then pin the top to main ScrollView otherwise to the last View.
        if(lastView == nil && index == 0) {
            [self.mainScroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[view]" options:0 metrics:nil views:@{@"view":view}]];
        }
        else {
            [self.mainScroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[lastView]-5-[view]" options:0 metrics:nil views:@{@"lastView":lastView, @"view":view}]];
        }

        [self.mainScroll addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.mainScroll attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
        [self.mainScroll addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];

        //--> If View is last then pin the bottom to mainScrollView bottom.
        if(index == arrayCount-1) {
            [self.mainScroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view]-5-|" options:0 metrics:nil views:@{@"view":view}]];
        }
        //--> Assign the current View as last view to keep the reference for next View.
        lastView = view;
    }
}