0
在我viewDidAppear
方法(在我打電話到超級方法),我有以下代碼:的UIScrollView不滾動 - 即使有效內容大小和滾動啓用
UIScrollView *navbar = [[UIScrollView alloc] init];
[navbar setScrollEnabled:YES];
[navbar setBackgroundColor:[UIColor redColor]];
[navbar setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:navbar];
NSDictionary *viewsDictionary2 = NSDictionaryOfVariableBindings(navbar);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[navbar]|"
options:0
metrics:nil
views:viewsDictionary2]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[navbar]|"
options:0
metrics:nil
views:viewsDictionary2]];
NSArray *categories = @[@"nav1", @"nav2", @"nav3", @"nav4", @"nav5", @"nav6", @"nav7", @"nav8", @"nav9", @"nav10", @"nav11", @"nav12", @"nav13"];
NSMutableArray *tempCategoryImages = [NSMutableArray array];
for (NSInteger i = 0; i < categories.count; i++)
{
[tempCategoryImages insertObject:[UIImage imageNamed:categories[i]] atIndex:i];
}
NSArray *categoryImages = [tempCategoryImages copy];
// Add subviews.
//
CGFloat xPadding = 25.0f;
UIView *previousImageView = NULL;
for (NSInteger i = 0; i < categoryImages.count; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithImage:categoryImages[i]];
[imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[navbar addSubview:imageView];
if (i == 0) {
//
// First category.
//
[navbar addConstraint:[NSLayoutConstraint constraintWithItem:imageView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:imageView.superview
attribute:NSLayoutAttributeLeft
multiplier:1
constant:0]];
} else {
//
// End categories.
//
[navbar addConstraint:[NSLayoutConstraint constraintWithItem:imageView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:previousImageView
attribute:NSLayoutAttributeRight
multiplier:1
constant:xPadding]];
}
[navbar addConstraint:[NSLayoutConstraint constraintWithItem:imageView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:imageView.superview
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];
previousImageView = imageView;
}
// Set content size.
//
CGSize scrollContentSize = CGSizeZero;
for (NSInteger i = 0; i < categories.count; i++)
{
UIImage *tempImage = [UIImage imageNamed:categories[i]];
// Width.
//
scrollContentSize.width += tempImage.size.width;
// Height.
//
if (tempImage.size.height > scrollContentSize.height) {
scrollContentSize.height = tempImage.size.height;
}
}
navbar.contentSize = scrollContentSize;
,當我登錄的scrollviews屬性,它具有子視圖,足夠大的內容大小和滾動功能。
即使我將UIScrollView
添加到IB並將其鏈接到相同的代碼,它仍然有效? (我註釋掉NSAutoLayout
代碼。
這使我相信,對於UIScrollView的工作,我不能使用自動佈局。
我缺少的東西?
感謝
編輯
該代碼也不能以編程方式工作,當我嘗試initWithFrame:
框架與setTranslatesAutoresizingMaskIntoConstraints:YES
我還沒有完全找到答案,我剛剛重寫了我的應用程序,所以我使用IB代替編碼。 –
檢查鏈接的示例:http://stackoverflow.com/a/16029013/400544 –