0

在我的viewDidAppear方法(我调用超级方法)中,我有以下代码:

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;

当我记录滚动视图属性时,它具有子视图、足够大的内容大小并启用了滚动。

即使我将其添加UIScrollView到 IB 并将其链接到相同的代码,它仍然可以工作吗?(我注释掉NSAutoLayout代码。

这让我相信 UIScrollView 工作,我不能使用自动布局。

我错过了什么吗?

谢谢

编辑

initWithFrame:当我尝试使用框架时,代码也无法以编程方式工作setTranslatesAutoresizingMaskIntoConstraints:YES

4

1 回答 1

0

Check the attributes inspector for your scrollview in IB. Make sure that bounce vertically is checked.

Fixed this problem for me.

于 2013-03-28T19:37:22.260 回答