1

我有一个启用了分页的 UIScrollView。我想用图像填充它,并且我有一个 UIImageView 需要在不使用.frame. 这是因为它不适用于启用自动布局。不使用自动布局不是一种选择。

这是它的代码:

//Prepare and load the view
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), (kScrollObjHeight))];
scrollView1.pagingEnabled = YES;
[scrollView1 setShowsHorizontalScrollIndicator:NO];

CGRect Rect = scrollView1.bounds;
UIImageView *beadContainer;

for (int i = 0; i < imageViews.count; i++)
{
    beadContainer = [imageViews objectAtIndex:i];
    beadContainer.frame = Rect;
    [scrollView1 addSubview:beadContainer];
    Rect.origin.x += Rect.size.width;
}

尽管 ScrollView 具有所有正确的尺寸并按预期滚动,但没有任何图像按原样显示。如果我注释掉beadContainer.frame = Rect;,那么数组中的所有图像imageViews都会出现在0, 0. 它们都出现在彼此之上。当然,我需要它们来填充 ScrollView。

4

2 回答 2

2

为什么不使用自动布局来布局图像?

//Prepare and load the view
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), (kScrollObjHeight))];
scrollView1.pagingEnabled = YES;
[scrollView1 setShowsHorizontalScrollIndicator:NO];



UIImageView *firstImage = imageViews[0];
UIImageView *lastImageView = [imageViews lastObject];
UIImageView *previousImageView = nil;
NSMutableArray *constraints = [NSMutableArray new];
for (UIImageView *imageView in imageViews){
    [scrollView1 addSubview:imageView];
    //set the size of the images
    [constraints addObject:[NSLayoutConstraint constraintWithItem:imageView
                                                        attribute:NSLayoutAttributeWidth
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:nil
                                                        attribute:NSLayoutAttributeNotAnAttribute
                                                       multiplier:1.0f
                                                         constant:kScrollObjWidth]];
    [constraints addObject:[NSLayoutConstraint constraintWithItem:imageView
                                                        attribute:NSLayoutAttributeHeight
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:nil
                                                        attribute:NSLayoutAttributeNotAnAttribute
                                                       multiplier:1.0f
                                                         constant:kScrollObjHeight]];
    //remove autoresizing masks
    [imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
    //pin the top of the imageview to the top of the superview
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintWithVisualFormat:@"V:|[imageView]"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:NSDictionaryOfVariableBindings(imageView)]];

    if ([firstImage isEqual:imageView]){ //pin the first image view to the left of the scrollview
        [constraints addObjectsFromArray:[NSLayoutConstraint constraintWithVisualFormat:@"|[firstImage]"
                                                                                options:0
                                                                                metrics:nil
                                                                                  views:NSDictionaryOfVariableBindings(firstImage)]];
    }

    if (previousImageView){ //pin any imageViews to the previous imageViews
        [constraints addObjectFromArray:[NSLayoutConstraint constraintWithVisualFormat:@"[previousImageView][imageView]"
                                                                               options:0
                                                                               metrics:nil
                                                                                 views:NSDictionaryOfVariableBindings(imageView, previousImageView)]];
    }
    previousImageView = imageView;
}

[scrollView1 addConstraints:constraints];

请注意,我没有尝试过,它可能是垃圾代码,我是在文本编辑器中编写的。

于 2012-09-27T13:24:09.000 回答
1

如果您要完全使用自动布局,请不要设置滚动视图的contentSize. 只需使用约束完全布置内容,确保描述每个内容子视图的大小,以及内容子视图与围绕它们的假想矩形边界的距离。运行时将自动使用该虚构的矩形来创建内容大小。

于 2012-11-26T18:51:35.563 回答