如何在AutoLayout(iOS6)中使用UIScrollView?

时间:2012-12-12 21:23:47

标签: uiscrollview ios6 autolayout

我正在尝试构建一个具有多个不同控件的视图,但我希望视图仅在垂直方向上滚动。我正在使用自动布局,所以我不想手动指定大小(我知道我可以这样做,但据我所知,根据UIScrollView上的发行说明你不应该这样做)

因此,如果我创建一个简单的视图控制器(没有.xib文件)并简单地将以下内容添加到init方法中,我希望我的标签会包装(他们这样做)并且我的视图会但是,垂直滚动,情况并非如此:

- (id)init {
    self = [super init];
    if (self) {

        UIScrollView *_scrollView = [UIScrollView new];
        [_scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];

        UILabel *label1 = [UILabel new];
        UILabel *label2 = [UILabel new];

        [label1 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [label2 setTranslatesAutoresizingMaskIntoConstraints:NO];

        [label1 setNumberOfLines:0];
        [label2 setNumberOfLines:0];

        [label1 setPreferredMaxLayoutWidth:240];
        [label2 setPreferredMaxLayoutWidth:240];

        NSString *sampleText = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
        [label1 setText:sampleText];
        [label2 setText:sampleText];

        [self.view addSubview:_scrollView];
        [_scrollView addSubview:label1];
        [_scrollView addSubview:label2];

        NSArray *constraints;
        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)];
        [self.view addConstraints:constraints];

        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)];
        [self.view addConstraints:constraints];

        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label1]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1)];
        [_scrollView addConstraints:constraints];

        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label2]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label2)];
        [_scrollView addConstraints:constraints];

        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label1]-[label2]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2)];
        [_scrollView addConstraints:constraints];
    }

    return self;
}

我觉得这个代码有几个结果很奇怪。如果您在viewDidLayoutSubviews方法(超级调用之后)中记录scrollviewheight,这是应用所有约束的地方,那么高度报告为0.还有一点奇怪的是,宽度报告的数字与I不同期待。我希望它能够报告超级视图的宽度,因为第一个约束(我甚至试图强制该约束中的宽度具有所谓的优先级,但它仍然不起作用)。

据我了解,UIScrollView的内容大小应根据其内容的内在大小设置,但似乎并未发生。

那么,有没有人知道缺少什么?

1 个答案:

答案 0 :(得分:9)

我已经联系了Apple的支持,以便回答我的问题。代码的问题是UIScrollView中包含的对象必须垂直固定到顶部和底部,以确定其内在内容大小。所以下面的代码行

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label1]-[label2]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2)];
[_scrollView addConstraints:constraints];

应改为:

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label1]-[label2]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2)];
[_scrollView addConstraints:constraints];

他们还指出我在几个对象上使用“new”关键字,其中基本的“init”不是指定的初始值设定项。虽然这与我的具体问题无关,但他们确实指出你应该避免这样做,因为你的对象可能处于无效状态。