UIScrollView与自动布局混合方法?

时间:2013-11-15 04:32:13

标签: ios uiscrollview autolayout

我正在尝试使用“混合方法”在apple dev上提及的示例代码link:

我试图在UIScrollView中垂直堆叠3个视图。在下面的示例中,UIScrollView在加载时显示红色视图,但不按预期滚动。我可以滚动一点点以查看红色视图下方的绿色视图 - 但滚动视图会弹回,并且不会滚动到绿色视图或其下方的视图(蓝色视图)。我知道我需要一个约束我试图在视图1和视图之间添加一个约束。 2使view2.top = view1.bottom,但似乎我遗漏了一些东西。

另外我注意到scrollview的内容大小为零(在viewDidAppear方法中)。

关于我所缺少的任何提示或如何使这种混合方法起作用的帮助将是非常好的!

    - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    UIScrollView* scrollView = ((UIScrollView*)self.view);

    scrollView.translatesAutoresizingMaskIntoConstraints = NO;

    CGFloat w = self.view.frame.size.width;
    CGFloat h = self.view.frame.size.height;

    contentView = [[UIView alloc] initWithFrame:CGRectMake(0,0, w, h*3)];

    [scrollView addSubview:contentView];


    v1 =[[UIView alloc] initWithFrame:CGRectMake(0,0, w, h)];
    v2 =[[UIView alloc] initWithFrame:CGRectMake(0,h, w, h)];
    v3 =[[UIView alloc] initWithFrame:CGRectMake(0,h*2, w, h)];

    v1.backgroundColor = [UIColor redColor];
    v2.backgroundColor = [UIColor greenColor];
    v3.backgroundColor = [UIColor blueColor];


    [contentView addSubview:v1];
    [contentView addSubview:v2];
    [contentView addSubview:v3];

    NSLayoutConstraint *myConstraint =[NSLayoutConstraint
                                       constraintWithItem:v1
                                       attribute:NSLayoutAttributeBottom
                                       relatedBy:NSLayoutRelationEqual
                                       toItem:v2
                                       attribute:NSLayoutAttributeTop
                                       multiplier:1.0
                                       constant:0];

    [contentView addConstraint:myConstraint];

    scrollView.contentSize = contentView.bounds.size;

}

2 个答案:

答案 0 :(得分:2)

如果您只是想在滚动视图中堆叠视图,我不明白为什么需要约束。我尝试了你的代码并且它不像你说的那样滚动得很好,但我认为它是由于你使用UIScrollView作为视图控制器的主视图而引起的,是否有一个特定的原因你想这样做?< / p>

我将代码更改为将UIScrollView添加到普通的UIView中,并且它可以按预期完美地工作,而不使用任何复杂的约束。请记住使用UIScrollView *scrollView;

在顶部定义滚动视图
- (void)viewDidLoad {
    [super viewDidLoad];

    scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:scrollView];

    CGFloat w = self.view.frame.size.width;
    CGFloat h = self.view.frame.size.height;

    contentView = [[UIView alloc] initWithFrame:CGRectMake(0,0, w, h*3)];
    [scrollView addSubview:contentView];

    v1 =[[UIView alloc] initWithFrame:CGRectMake(0,0, w, h)];
    v2 =[[UIView alloc] initWithFrame:CGRectMake(0,h, w, h)];
    v3 =[[UIView alloc] initWithFrame:CGRectMake(0,h*2, w, h)];

    v1.backgroundColor = [UIColor redColor];
    v2.backgroundColor = [UIColor greenColor];
    v3.backgroundColor = [UIColor blueColor];

    [contentView addSubview:v1];
    [contentView addSubview:v2];
    [contentView addSubview:v3];

    scrollView.contentSize = contentView.bounds.size;
}

答案 1 :(得分:0)

我不得不为UIScrollView的愚蠢的纯粹自动布局方法付出很多努力。我想要一个基于自动布局约束的子视图的滚动视图,并且可以使用旋转。最后,下面的“混合方法”代码对我来说是垂直滚动并保持简单:

 - (void)viewDidLoad {
    UIView *contentView;

    contentView = [[UIView alloc] initWithFrame:self.scrollView.bounds];

    //don't add UIViewAutoresizingFlexibleHeight as that messes things up but next line helps with rotation
    contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

    [scrollView addSubview:contentView];

    // DON'T change contentView's translatesAutoresizingMaskIntoConstraints,
    // which defaults to YES;

    // Set the content size of the scroll view to match the size of the content view
    [scrollView setContentSize:CGSizeMake(contentWidth, contentHeight)];

     /* the rest of your code here... pre auto-layout style and you can change the origins of subviews to position them within contentView */
      [contentView addSubview:subView1];
      [contentView addSubview:subView2];
    }

要实现的关键点是,不要在初始化之后的任何时候更改contentView的大小并将其添加到scrollview,因为这会导致其内部约束中断,但您仍然可以继续添加子视图而不必担心其大小。< / p>

您将独立于contentView的大小计算滚动视图内容大小,除非您使用我尚未测试的systemLayoutSizeFittingSize:方法