4

I am making some sort of "form" with many input fields (some are textfields, some are switches, some gather info from pickers etc).
There are 6 sections in this form, and the nature of some answers influences the rest of the interface (for example, if you select that you own a car, more options will show below that).

In order to accomplish that, I started making a very large view (mainView) that is subview of a UIScrollView, however it was getting too big so I decided to create one nib file for each section. I set the file owner of each nib file to my MainFormViewController, and then I create an outlet for each view: section1View, section2View etc. I load the section views in -viewDidLoad like this:

// Section 1
UINib *nib1  = [UINib nibWithNibName:@"Section1" bundle:nil];
[nib1 instantiateWithOwner:self options:nil];
CGRect frame1 = self.section1View.frame;
frame1.origin.y = 10;
[self.section1View setFrame:frame1];
[self.mainView addSubview:self.section1View]; // mainView is the one I add to the scrollView

// Section 2 (goes 10px below section 1)
UINib *nib2  = [UINib nibWithNibName:@"Section2" bundle:nil];
[nib2 instantiateWithOwner:self options:nil];
CGRect frame2 = self.section2View.frame;
frame2.origin.y = frame1.origin.y + frame1.size.height + 10;
[self.section2View setFrame:frame2];
[self.mainView addSubview:self.section2View];

// Same approach for all other sections
// ...

Now this seems to work fine, however my problem is that when I change the height of these section subviews, I can't get the rest of the subviews to adapt to the height change. For example, if I change the height of the first section:

CGRect mainFrame = self.mainView.frame;
CGRect section1Frame = self.section1View.frame;
section1Frame.size.height -= 150;
mainFrame.size.height -= 150;
[UIView animateWithDuration:0.3 animations:^(){
    self.section1View.frame = section1Frame;
    self.mainView.frame = mainFrame;
    self.scrollView.contentSize = mainFrame.size;
} completion:^(BOOL finished){
    //
}];

The rest of the views (section2View, section3View etc) do not "follow" the change in the frame of the mainView, whatever orientation I tried. To my understanding, changing the frame of the mainView should influence the frame of its subviews, according to the subviews' autoresizing options, right? In my case the subviews just stay in the same place.
The "Autoresize Subviews" option of the main view in Interface Builder is selected.

Can anybody help me out with that?

Forge
  • 6,538
  • 6
  • 44
  • 64
phi
  • 10,634
  • 6
  • 53
  • 88
  • Changing the size of a scrollView doesn't resize the subviews, it simply scrolls more (or less) to see it. That is what a scrollView is designed for: to scroll! :) – lnafziger Apr 04 '12 at 14:28
  • @Inafziger However, changing the size of scrollView.contentSize will resize the subviews.... – Sulthan Apr 04 '12 at 14:36

3 Answers3

5

Are you expecting the other sections to go 150pxs up? I think there is no autoresizing mask that can accomplish that.

If your section2View has fixed top margin, even if its superview's size changes it won't move. If its top margin is flexible and bottom margin is flexible, it will move, but only 75pxs. If only top margin is flexible, it will move up correctly but that will work only when section 1 is changed.

Summary: You have to fix the position of all of your sections manually when changing the size of one of them. If you put the sections into a NSArray, it will get really easy.

Or better - use a UITableView which is perfect for this situation.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • Thanks for the answer. It seems I had misunderstood the concept of autoresizing masks, but you helped me do some experiments that really made things clear :) – phi Apr 07 '12 at 10:55
3

Loading a view from a nib should not have any autoresizing issues. I suspect that your autoresize mask is incorrect.

With that said, I put together a sample project to test this out. I don't know exactly what affect you are going for, but I put the sample on github so you can take a look and see if it helps you address your issue.

https://github.com/jdecarlo/StackOverflow9920412

If the sample does not help you solve your problem, add a comment to this answer explaining why and I'll modify it.

Joseph DeCarlo
  • 3,268
  • 23
  • 28
  • His mainView is a subclass of a scroll view, and not a view controller like yours. Great example of what he should be doing though! – lnafziger Apr 04 '12 at 14:31
  • @Inafziger my mainView is NOT a subclass but a subview of the scroll view. Thanks a lot for the effort Joseph. I'll try to modify your project to fit me needs and come back to you in a while. – phi Apr 07 '12 at 11:03
  • So, after trying the code, I think this is not exactly what I meant. Imagine a scenario where by removing one of the images in your example, whatever is below that image repositions its self higher. +1 for the effort though, thanks! – phi Apr 07 '12 at 16:22
1

Are you sure the autoresizing masks are set properly? To check this, use a breakpoint somewhere after all the views and subviews are created and type this command into GDB window:

po [[self mainView] recursiveDescription]

it will show you the whole hierarchy of views and subviews with all their frames and autoresizing masks.

kuba
  • 7,329
  • 1
  • 36
  • 41