【问题标题】:ContentView inside the Scroll View not increasing the heightScroll View 内的 ContentView 不增加高度
【发布时间】:2015-11-17 05:00:59
【问题描述】:

我为我的ViewController设置了以下约束

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

-(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 中,我可以看到高度正在变化。

CONTENT VIEW FRAME HEIGHT==== 8913.000000

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

【问题讨论】:

  • 我认为将这一行从 for 循环 self.mainScroll.contentSize = total height 中取出并将其更改为上述行并在 for 循环之后调用 [self.view layoutsubviews] 方法。

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


【解决方案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;
    }
}

【讨论】:

  • 感谢您的解决方案。我试过了,但只有前 2 个视图添加到滚动视图中。并且滚动视图现在不可滚动。这是为什么呢?
  • 这是一个工作代码,因为我在我的项目中使用它。您从情节提要中删除了ContentView,对吗?您是否在调试器窗口中看到任何冲突的约束?如果是的话,也在这里发布。
  • 是的,我删除了它。而且我在调试器窗口中看不到任何冲突的约束。
  • 我可以水平滚动。但是没有垂直移动,我可以在视图上看到第一个 2 个视图,而且这 2 个视图之间的间隙(垂直间隙)似乎超过 5。它大约 100
  • 嗯,没有多大意义。如果您可以分享代码,我可以提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多