Reputation: 2735
I have a UIScrollView with paging enabled. I want to full it with images and I have a UIImageView which needs to be made to fit without using .frame
. This is because it doesn't work with Autolayout enabled. Not using Autolayout isn't an option.
Here's the code as it stands:
//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;
}
None of the images appear as it stands, though the ScrollView has all of the right dimensions and scrolls as expected. If I comment out beadContainer.frame = Rect;
, then all of the images in the array imageViews
appear at 0, 0
. They all show up on top of one another. Of course, I need them to fill the ScrollView.
Upvotes: 1
Views: 1771
Reputation: 535306
If you are going to use auto layout fully, do NOT set the scroll view's contentSize
. Just lay out the contents fully with constraints, making sure to describe the size of every one of the contents subviews, and the distance of the contents subviews from the bounds of an imaginary rectangle surrounding them. The runtime will automagically use that imaginary rectangle to create the content size.
Upvotes: 1
Reputation: 10294
Why don't you use autolayout to layout the images?
//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];
Mind you I haven't tried this, it might be rubbish code, I wrote it in a text editor.
Upvotes: 2