自动布局+ UIScrollView和单个子视图

时间:2015-03-14 02:54:41

标签: ios uiscrollview autolayout

只需使用自动布局和UIScrollView。我在这里错过了什么?这会在屏幕底部显示蓝色scr​​ollView但我看不到contentView。视图调试器也无话可说。有任何想法吗?

//
//  ViewController.m
//  Fit
//
//  Created by Adam Dahan on 2015-03-13.
//  Copyright (c) 2015 Adam Dahan. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    UIScrollView *scrollView;
    UIView *contentView;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self createViews];
    [self constrainScrollView];
    [self constrainContentView];
}

- (void)createViews
{
#pragma Initialize a scrollView
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    scrollView.backgroundColor = [UIColor blueColor];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollView];

#pragma Initialize a contentView for the scrollView
    contentView = [[UIView alloc] initWithFrame:CGRectZero];
    contentView.backgroundColor = [UIColor redColor];
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [scrollView addSubview:contentView];
}

- (void)constrainScrollView
{

#pragma scrollView vertical constraints
    NSArray *cns = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[scrollView(40)]|"
                                                       options:0
                                                       metrics:nil
                                                                views:NSDictionaryOfVariableBindings(scrollView)];
    [self.view addConstraints:cns];

#pragma scrollView horizontal constraints
    cns = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|"
                                              options:0
                                              metrics:nil
                                                views:NSDictionaryOfVariableBindings(scrollView)];
    [self.view addConstraints:cns];
}

- (void)constrainContentView
{

#pragma contentView vertical constraints
    NSArray *cns = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|"
                                              options:0
                                              metrics:nil
                                                views:NSDictionaryOfVariableBindings(scrollView, contentView)];
    [scrollView addConstraints:cns];

#pragma contentView horizontal constraints
    cns = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|"
                                              options:0
                                              metrics:nil
                                                views:NSDictionaryOfVariableBindings(scrollView, contentView)];
    [scrollView addConstraints:cns];
}

@end

2 个答案:

答案 0 :(得分:1)

你真的应该看看Technical Note TN2154 UIScrollView And Autolayout

您的方式让系统无法决定scrollView的contentSize。

主要想法是让scrollView知道contentSize是什么,无论你选择哪种方式。

答案 1 :(得分:1)

使用自动布局时,ScrollViews有点复杂。框架是根据约束与其他视图定义的(对于您将在constainScrollView方法中添加的那些)。

contentSize的定义取决于scrollView内视图的约束。对于您的情况,contentSize是(0,0)因为内部视图没有宽度或高度约束。

我想你希望这个内部视图填充scrollview。那么你的约束应该是这样的:

- (void)constrainContentView
{
    [self.view layoutIfNeeded]; // This causes the scrollview width and height to be calculated based on the constraints you sent in the previous method
    // You could check by adding a breakpoint here, if the scrollview has the frame you would expect

#pragma contentView vertical constraints
    NSArray *cns = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[contentView(height)]-|"
                                              options:0
                                              metrics:@{"height":@(CGRectGetHeight(scrollView.frame))}
                                                views:NSDictionaryOfVariableBindings(scrollView, contentView)];
    [scrollView addConstraints:cns];

#pragma contentView horizontal constraints
    cns = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[contentView(width)]-|"
                                              options:0
                                              metrics:@{"width":@(CGRectGetWidth(self.view.frame))}
                                                views:NSDictionaryOfVariableBindings(scrollView, contentView)];
    [scrollView addConstraints:cns];
}

您为widthheight设置的实际值取决于您的需求。

重要的是,scrollView中的视图需要以这样的方式设置约束,以便可以毫不含糊地计算contentSize。这意味着如果视图没有固有大小(如UIButtonUILabel),则需要明确添加宽度/高度约束。

由于明确设置了宽度和高度,因此在方向更改时,您需要更新约束,以便内部视图具有正确的尺寸。

希望这可以解决您的问题。