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

标签 uiscrollview ios6 autolayout

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

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

- (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 中记录 ScrollView 高度方法(在 super 调用之后),这是应该应用所有约束的地方,然后高度报告 0。同样奇怪的是,宽度报告的数字与我预期的不同。由于第一个约束,我希望它报告 super View 的宽度(我什至尝试以据称需要的优先级强制该约束内的宽度,但它仍然没有工作)。

据我了解,UIScrollView的内容大小应该由其内容的固有大小设置,但这似乎没有发生。

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

最佳答案

我已经联系了 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];

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

关于uiscrollview - 如何在 AutoLayout (iOS6) 中使用 UIScrollView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13848895/

相关文章:

iphone - 从通知中心删除本地通知 (iOS 6)

objective-c - iOS 在设备旋转时更改自动布局约束

ios - 出现键盘时如何使 View Controller 滚动到文本字段

uiscrollview - 以编程方式将 UILabels 和 UIImageViews 添加到 UIScrollView

ios - 如何使按钮在 UIScrollView 内滚动?

ios - 人类如何理解 Xamarin.iOS Storyboard设计器的模棱两可的布局约束部分?

ios - AutoLayout 约束以适应矩形内的 View ,保留一定的纵横比(以编程方式)

UIScrollView 总是回弹

ios - 如何从两个文本字段中获取值?

ios - '+ entityForName:nil不是搜索实体名称的合法NSManagedObjectContext参数