如何使用带有项目格式的约束在scrollview上添加容器视图

时间:2015-09-06 15:17:04

标签: ios swift autolayout

我是自动布局的新手,我正在尝试使用container view" ScrollView"在auto-layouts上添加一个constraint with item但是我无法使用我的代码添加。

我知道如何使用 visual formate添加,但我想使用constraint with item formate来完成此过程。请帮帮我!

我的代码:

#import "ViewController3.h"

@interface ViewController3 ()
{
    UIScrollView * scrollView;
    UIView * containerView;
}

@end

@implementation ViewController3


- (void)viewDidLoad {
    [super viewDidLoad];

    scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor lightGrayColor];
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:scrollView];

    containerView = [[UIView alloc] init];
    containerView.backgroundColor = [UIColor orangeColor];
    containerView.translatesAutoresizingMaskIntoConstraints = NO;
    [scrollView addSubview:containerView];

    //Applying autolayouts for scrollview
    NSLayoutConstraint * constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
    [self.view addConstraint:constraint1];

    constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
    [self.view addConstraint:constraint1];

    constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
    [self.view addConstraint:constraint1];

    constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
    [self.view addConstraint:constraint1];

    //Applying autolayouts for containerview
    NSLayoutConstraint * constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: scrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
    [scrollView addConstraint:constraint2];

    constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
    [scrollView addConstraint:constraint2];

    constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
    [scrollView addConstraint:constraint2];

    constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
    [scrollView addConstraint:constraint2];
}

1 个答案:

答案 0 :(得分:2)

使用Autolayout,您无需明确设置scrollview的contentSize,但您必须提供足够的线索让Autolayout系统推断出您的scrollview的contentSize究竟是什么。

通常有两种方法可以使用Autolayout,混合或纯Autolayout布局UIScrollView。请检查:https://developer.apple.com/library/ios/technotes/tn2154/_index.html

如果它是纯粹的Autolayout,您可以通过containerView的子视图实现这一点,也就是说,您的子视图不依赖于滚动视图来获取它们的大小。或者,您可以将containerView的宽度和高度绑定到滚动视图外的任何视图,例如self.view,或者固定值。

E.g, 您可以再添加四行:

constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
[self.view addConstraint:constraint2];

constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
[self.view addConstraint:constraint2];