Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected behavior when autoresizing subviews loaded from nibs

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?

like image 932
phi Avatar asked Jan 17 '23 01:01

phi


1 Answers

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.

like image 189
Sulthan Avatar answered Jan 21 '23 01:01

Sulthan