0

I have setup a viewcontroller using storyboard + autolayout, however I am having some problems (re)sizing a subview correctly for 3.5"/4" display.

This is what I have setup in storyboard:

Storyboard

The scrollview correctly keeps its 20px space to the bottom according to the constraint. However the subviews (red), which I add in the below loop to the scrollview should have the same height as the scrollview, but they don't :(

int pageCount= 3;

self.scrollView.delegate= self;
self.scrollView.pagingEnabled= YES;
self.scrollView.scrollEnabled= YES;
self.scrollView.showsHorizontalScrollIndicator= NO;
self.scrollView.showsVerticalScrollIndicator= NO;

self.scrollView.contentSize= CGSizeMake(self.scrollView.bounds.size.width * pageCount, self.scrollView.bounds.size.height);

NSLog(@"viewcontroller frame: %@ (bounds: %@)", NSStringFromCGRect(self.view.frame), NSStringFromCGRect(self.view.bounds));
NSLog(@"scrollview frame: %@ (bounds: %@)", NSStringFromCGRect(self.scrollView.frame), NSStringFromCGRect(self.scrollView.bounds));
NSLog(@"scrollview content size%@", NSStringFromCGSize(self.scrollView.contentSize));

 for (int i= 0; i < pageCount; i++)
 {
  UIView* neutralContainer= [[UIView alloc] initWithFrame:CGRectMake(0.0 + i * self.scrollView.frame.size.width, 0.0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
  neutralContainer.backgroundColor= [UIColor redColor];

  NSLog(@"containerview frame: %@ (bounds: %@)", NSStringFromCGRect(neutralContainer.frame), NSStringFromCGRect(neutralContainer.bounds));
  [self.scrollView addSubview:neutralContainer];

As it seems, the scrollview frame i print in console (see screenshot) remains the same, no matter if i start the 4" or the 3.5" simulator. i would expect to get different values.
Can someone explain to me what i am doing wrong?

user826955
  • 3,137
  • 2
  • 30
  • 71

2 Answers2

0

Try setting your scrollview content size.

self.scrollView.contentSize.Height = self.scrollView.frame.size.height;

Your subview is being added dynamically, and does not have to conform to its parent's constraints.

Theopile
  • 868
  • 3
  • 14
  • 30
0

Coming across this post Storyboard UIScrollView contentSize? helped me solve the problem:
Instead of initializing the subviews for UIScrollview in the viewDidLoad method, I moved the code to viewDidLayoutSubviews and voila, the content size is correct for both 3.5" and 4", and the subviews are layed out correctly within my scrollview.

Community
  • 1
  • 1
user826955
  • 3,137
  • 2
  • 30
  • 71