我的自动布局滚动视图中的约束有什么问题?

时间:2014-11-13 10:27:14

标签: ios uiscrollview autolayout

我在尝试修复scrollView的自动布局时非常困难。我确保在scrollView下有一个内容视图,并使内容视图与mainView具有相同的宽度和高度。但是当我运行应用程序时,只显示主视图(superview)。为了更好地说明我的情况,我有以下屏幕截图:

screenshot http://oi58.tinypic.com/3128e9h.jpg

1 个答案:

答案 0 :(得分:0)

这就是我在代码中的表现:

ViewController.h

@interface ViewController : UIViewController

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UILabel *toolBoxView;

@end

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self initViews];
    [self initConstraints];
}

-(void)viewWillLayoutSubviews
{
    // ------------------------------------------------------------
    // This line is required to prevent text label from
    // forcing scrollView to be horizontally format
    // ------------------------------------------------------------
    self.toolBoxView.preferredMaxLayoutWidth = self.view.bounds.size.width - 40.0;
}

-(void)initViews
{
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.layer.borderColor = [UIColor redColor].CGColor;
    self.scrollView.layer.borderWidth = 1.0;

    self.contentView = [[UIView alloc] init];
    self.contentView.layer.borderColor = [UIColor blueColor].CGColor;
    self.contentView.layer.borderWidth = 1.0;

    self.toolBoxView = [[UILabel alloc] init];
    self.toolBoxView.layer.borderColor = [UIColor greenColor].CGColor;
    self.toolBoxView.layer.borderWidth = 1.0;
    self.toolBoxView.textColor = [UIColor blackColor];
    self.toolBoxView.text = @"This is the toolbox view. Which resides within the contentView of a UIScrollView. The scrollView will adjust to fit the content of the text within this toolBoxView. Feel free to change this text as you see fit\n\nThis is the toolbox view. Which resides within the contentView of a UIScrollView. The scrollView will adjust to fit the content of the text within this toolBoxView. Feel free to change this text as you see fit\n\nThis is the toolbox view. Which resides within the contentView of a UIScrollView. The scrollView will adjust to fit the content of the text within this toolBoxView. Feel free to change this text as you see fit\n\nThis is the toolbox view. Which resides within the contentView of a UIScrollView. The scrollView will adjust to fit the content of the text within this toolBoxView. Feel free to change this text as you see fit\n\n";
    self.toolBoxView.numberOfLines = 0;
    self.toolBoxView.lineBreakMode = NSLineBreakByWordWrapping;



    [self.contentView addSubview:self.toolBoxView];
    [self.scrollView addSubview:self.contentView];

    [self.view addSubview:self.scrollView];
}

-(void)initConstraints
{
    self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
    self.toolBoxView.translatesAutoresizingMaskIntoConstraints = NO;

    id views = @{
                 @"scrollView": self.scrollView,
                 @"contentView": self.contentView,
                 @"toolBoxView": self.toolBoxView
                 };

    // scrollView constraints

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:nil views:views]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics:nil views:views]];



    // contentView constraints (need to pin to all four sides of scrollView)

    [self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[contentView]-10-|" options:0 metrics:nil views:views]];
    [self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[contentView]-10-|" options:0 metrics:nil views:views]];


    // toolBoxView constraints

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[toolBoxView]-10-|" options:0 metrics:nil views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[toolBoxView]-10-|" options:0 metrics:nil views:views]];
}

这就是我得到的:

screenshot

真正奇怪的是,为什么Xcode没有说出自动布局冲突,因为如果你看看我上面的代码,我已经将scrollView的四个边缘固定到viewController& #39; s视图但同时,contentView强烈固定到scrollView的所有四个边缘,toolBoxView也强烈固定到contentView的边缘。从理论上讲,如果toolBoxView比scrollView更短或更高,则会导致冲突。

我想这就是UIScrollView在autolayout中的工作方式。

如果我改变:

V:|[scrollView]|

V:|[scrollView]

我只看到一个空白的白页(即你描述为"只显示主视图")。

希望我编写的上述限制可以帮助您调试Interface Builder约束,也许您可​​以将自己与我的进行比较。

希望有所帮助。