這是我第一次使用UIScrollViews純粹的Autolayout方法。這是視圖層次結構的樣子iOS - 純AutoLayout和UIScrollView不滾動
view
-scrollview
--view1
--view2
--view3
scrollview應該按順序包含view1 | view2 | view3。
我將scrollviews的寬度,高度,centerx和底部空間設置爲superview。創建的view1,view2和view3都在其updateConstraints方法中設置了寬度和高度約束。此外,代碼中提供了一些限制。這個scrollview不是從左到右滾動的原因是什麼?從字面上看,我可以在網上找到關於以自動佈局以編程方式創建和添加子視圖到UIScrollView的所有指南。我發現一些提到必須爲每個視圖提供四個不同的約束,前導,尾隨,頂部和底部作爲子視圖添加到滾動視圖。這些是唯一可以指定的NSLayoutAttributes嗎?屬性如NSLayoutAttribueLeft或NSLayoutAttribueRight如何相關?我也閱讀過蘋果網站上的文檔,特別是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中設置。 – 2014-08-31 11:21:41
這不會改變任何東西。我相信有一些約束是不會被添加的。我只是不知道是什麼。 – kamran619 2014-08-31 17:37:34