[英]XCode autolayout not working with UIScrollView for horizontal paging
I'm trying to create a horizontal paging by using AutoLayout. 我正在尝试通过使用AutoLayout创建水平分页。 Here's the code.
这是代码。
UIView *newsView = [[UIView alloc] initWithFrame:CGRectZero];
newsView.backgroundColor = [UIColor redColor];
UIView *anotherNewsView = [[UIView alloc] initWithFrame:CGRectZero];
anotherNewsView.backgroundColor = [UIColor blueColor];
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
newsView.translatesAutoresizingMaskIntoConstraints = NO;
anotherNewsView.translatesAutoresizingMaskIntoConstraints = NO;
self.scrollView.pagingEnabled = YES;
[self.scrollView addSubview:newsView];
[self.scrollView addSubview:anotherNewsView];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:newsView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:0.0f]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:newsView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.0f]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:newsView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:0.0f]];
//
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:newsView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:anotherNewsView
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:0.0f]];
//
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:anotherNewsView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:0.0f]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:anotherNewsView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.0f]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:anotherNewsView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:0.0f]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:newsView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:anotherNewsView
attribute:NSLayoutAttributeWidth
multiplier:0.99f
constant:0.0f]];
But I only see the scrollView
which I set the background to cyan. 但是我只看到将背景设置为青色的
scrollView
。
The problem is that your views have width and height zero as they are configured right now. 问题在于您的视图的宽度和高度为零,因为它们现在已配置。
Try adding some with and height constraints to them like : 尝试为它们添加一些具有高度限制的对象,例如:
NSArray *heightConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view(height)]" options:0 metrics:@{ @"height":@(CGRectGetHeight(self.scrollView.frame)) } views:@{ @"view":newsView }];
[self.scrollView addConstraints: heightConstraints];
[self.scrollView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:[newsView(width)]" options:0 metrics:@{ @"width":@(CGRectGetWidth(self.scrollView.frame)) } views:@{ @"newsView":newsView]];
[self.scrollView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:[anotherNewsView(width)]" options:0 metrics:@{ @"width":@(CGRectGetWidth(self.scrollView.frame)) } views:@{ @"anotherNewsView":anotherNewsView }]];
You can also check one of my other answers here . 您也可以在这里查看我的其他答案之一。
The important thing is that the views inside the scrollview
must have their own width and height. 重要的是,
scrollview
内部的scrollview
必须具有自己的宽度和高度。 This is how the scrollView
calculates it's contentSize
. 这就是
scrollView
计算它的contentSize
。 If you don't have that, the contentSize
will be CGSizeZero
and you won't see your views. 如果没有,
contentSize
将为CGSizeZero
并且您将看不到视图。
Hope this helps! 希望这可以帮助! Let me know if you need more help.
让我知道您是否需要更多帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.