只是玩弄自动布局和 UIScrollView。我在这里错过了什么?这会在屏幕底部显示一个蓝色的 scrollView,但我看不到 contentView。 View 调试器也无话可说。有任何想法吗?
//
// 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
最佳答案
使用自动布局时, ScrollView 会稍微复杂一些。框架是根据其他 View 的约束定义的(因为您将是在 constainScrollView
方法中添加的 View )。
但是 contentSize
是根据 scrollView
内部 View 的约束定义的。对于您的情况,contentSize 是 (0,0),因为内部 View 没有宽度或高度限制。
我想您希望这个内部 View 填充 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];
}
您为 width
和 height
设置的实际值取决于您的需要。
重要的是 scrollView
内的 View 需要以这样的方式设置约束,以便可以毫无歧义地计算 contentSize
。这意味着如果 View 没有固有大小(如 UIButton
或 UILabel
),您需要显式添加宽度/高度约束。
由于显式设置宽度和高度,在方向更改时您需要更新约束,以便内部 View 具有正确的尺寸。
希望这能解决您的问题。
关于ios - 自动布局 + UIScrollView 和单个 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29044917/