UIImageView定义大小而不使用.frame

时间:2012-09-14 01:17:54

标签: ios6 iphone-5 xcode4.5

我有一个启用了分页的UIScrollView。我想用图像填充它,我有一个UIImageView,需要在不使用.frame的情况下进行调整。这是因为它不支持启用Autolayout。不使用Autolayout不是一个选择。

这里是代码:

//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。

2 个答案:

答案 0 :(得分: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];

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

答案 1 :(得分:1)

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