【发布时间】:2014-10-24 15:28:49
【问题描述】:
这是我第一次使用带有纯自动布局方法的 UIScrollViews。这就是视图层次结构的样子
view
-scrollview
--view1
--view2
--view3
scrollview 应按该顺序包含 view1|view2|view3。
我将滚动视图的宽度、高度、中心和底部空间设置为超级视图。创建的 view1、view2 和 view3 都在其 updateConstraints 方法中设置了宽度和高度约束。此外,代码中提供了一些约束。这个滚动视图没有从左到右滚动的原因是什么?我已经阅读了我可以在网上找到的所有关于以编程方式使用自动布局创建子视图并将子视图添加到 UIScrollView 的指南。我发现有人提到必须为作为子视图添加到滚动视图的每个视图提供四种不同的约束,前导、尾随、顶部和底部。这些是唯一可以指定的 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;
}
打印出滚动视图约束后,我觉得它们看起来不错:
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>
)
下面是它的截图:
我觉得奇怪的是数据源中的最后一个元素是显示在滚动视图中的第一个视图控制器,而它应该是最后一个视图。它也不会像应有的那样从左向右滚动。
【问题讨论】:
-
尝试从 viewWillAppear 调用 initView,因为您的布局的所有尺寸都未在 viewDidLoad 中设置。
-
这不会改变任何东西。我相信有一些限制没有被添加..我只是不知道是什么。
标签: ios ios7 uiscrollview autolayout