这是我第一次使用纯粹的Autolayout方法使用UIScrollViews。这就是视图层次结构的样子
view
-scrollview
--view1
--view2
--view3
scrollview应按顺序包含view1 | view2 | view3。
我将scrollviews的宽度,高度,中心和底部空间设置为superview。创建的view1,view2和view3都在其updateConstraints方法中设置了宽度和高度约束。另外,代码中提供了一些约束。这个scrollview不是从左向右滚动的原因是什么?我已经在网上找到了所有可以在网上找到的关于使用自动布局以编程方式创建和添加子视图到UIScrollView的指南。我发现有一些提及必须提供四个不同的约束,每个视图的前导,尾随,顶部和底部作为子视图添加到scrollview。这些是唯一可以指定的NSLayoutAttributes吗? NSLayoutAttribueLeft或NSLayoutAttribueRight等属性如何关联?我也在Apples网站上阅读过文档,特别是https://developer.apple.com/library/ios/technotes/tn2154/_index.html。我正在附加我目前的设置。一切都是通过代码完成的。
- (void)viewDidLoad
{
[super viewDidLoad];
self.dataSource = @[ [[PCCGenericRating alloc] initWithTitle:@"Easiness"
andMessage:@"WHAT A JOKERRRR"
andVariatons:@[ @"very easy", @"easy", @"moderate", @"hard", @"very hard"]],
[[PCCGenericRating alloc] initWithTitle:@"Joker"
andMessage:@"WHAT A JOKERRRR"
andVariatons:@[ @"very easy", @"easy", @"moderate", @"hard", @"very hard"]],
[[PCCGenericRating alloc] initWithTitle:@"Difficulty"
andMessage:@"YOu are not difficult at all"
andVariatons:@[ @"very easy", @"easy", @"moderate", @"hard", @"very hard"]]
];
[self initView];
}
- (void)initView {
CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
CGFloat heightDifference = navigationBarHeight + statusBarHeight;
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.delegate = self;
[self.scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
self.scrollView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.scrollView];
//setup constraints
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:1.0f
constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:1.0f
constant:-heightDifference]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.0]];
[self.dataSource enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
PCCGenericRating *rating = (PCCGenericRating *)obj;
PCCGenericRatingView *ratingView = [self createViewWithRating:rating];
[self.scrollView addSubview:ratingView];
int multiplier = (idx == 0) ? 1 : (int) (idx + 1) ;
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:ratingView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeCenterX
multiplier:multiplier
constant:0.0f]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:ratingView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0f]];
}];
}
- (PCCGenericRatingView *)createViewWithRating:(PCCGenericRating *)rating {
PCCGenericRatingView *view = [PCCGenericRatingView genericRatingViewWithTitle:rating.title andMessage:rating.message];
return view;
}
在打印出scrollview约束后,它们对我来说没问题:
po self.scrollView.constraints
<__NSArrayM 0x115b051f0>(
<NSLayoutConstraint:0x1145d9290 PCCGenericRatingView:0x114579880.centerX == UIScrollView:0x11458d4b0.centerX>,
<NSLayoutConstraint:0x1145d9410 PCCGenericRatingView:0x114579880.centerY == UIScrollView:0x11458d4b0.centerY>,
<NSLayoutConstraint:0x1145d9dd0 PCCGenericRatingView:0x1145d9560.centerX == 2*UIScrollView:0x11458d4b0.centerX>,
<NSLayoutConstraint:0x1145d9e40 PCCGenericRatingView:0x1145d9560.centerY == UIScrollView:0x11458d4b0.centerY>,
<NSLayoutConstraint:0x1145da6b0 PCCGenericRatingView:0x1145d9e90.centerX == 3*UIScrollView:0x11458d4b0.centerX>,
<NSLayoutConstraint:0x1145da730 PCCGenericRatingView:0x1145d9e90.centerY == UIScrollView:0x11458d4b0.centerY>
)
以下是它的外观截图:
我觉得奇怪的是数据源中的最后一个元素是第一个在滚动视图中显示的视图控制器,它应该是最后一个视图。它也不会按原样从左向右滚动。
答案 0 :(得分:4)
确保 view1 的top_constraint
和 view3 的bottom_constraint
符合 scrollView的约束 。否则请滚动查看contentSize: {0, 0}
。
答案 1 :(得分:3)
无论您在何处打印约束,请尝试打印scrollview.contentSize,它可能是0,0,这就是您的问题所在。据我所知,正如您在帖子中提到的,您必须将滚动视图的子视图显式设置为滚动视图左上角和右下角约束。设置这些会自动设置scrollview的contentSize,使其能够滚动。看起来您只设置了centerX和centerY约束,这些约束不会将scrollviews contentSize设置为您需要的内容。
尝试以编程方式设置这些(这是伪代码,但你明白了):
如果您正确设置了所有这些,则滚动视图将正确滚动。只记得检查contentsize,如果contentsize是0,0那么你的约束没有正确设置。