Unexpected behavior when autoresisting subpoints loaded from knives

I am doing some sort of "form" with many input fields (some are text fields, some are radio buttons, some are collecting information from collectors, etc.). There are 6 sections in this form, and the nature of some of the responses affects the rest of the interface (for example, if you choose that you have a car, it will show more options).

In order to do this, I started doing a very large overview ( mainView

) which is subclassed UIScrollView

, however it was getting too large, so I decided to create one nib file per section. I set the file owner of each nib file to mine MainFormViewController

and then create an output for each view: section1View, section2View, etc. I am loading sections in the -viewDidLoad

following way:

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

      

This works fine now, however my problem is that when I change the height of these sections, I cannot get the rest of the subplots to adapt to the change in height. 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" changes in the frame mainView

, regardless of the orientation I tried. As I understand it, changing the frame mainView

should affect the frame of its subzones, according to the autosize parameters of the subviews, right? In my case, the spies just stay in the same place. The "Autoresize Subviews" option of the main view in Interface Builder is selected. Can anyone help me with this?

+3


source to share


3 answers


Do you expect other sections to be 150px up? I think there is no auto-resist mask that can do this.

If yours section2View

has a fixed top margin, even if its supervisor size changes, it won't move. If its top edge is flexible and the bottom margin is flexible, it will move, but only 75 pixels. If only the top edge is flexible it will move correctly, but this will only work when changing section 1.



Summary: You have to fix the position of all your sections manually when you resize one of them. If you put the sections in NSArray

, it will be very easy.

Or better, use the UITableView

one that's perfect for the situation.

+5


source


Loading a view from anything shouldn't have autoresistance issues. I suspect your autoresist mask is incorrect.

With that said, I put together a sample project to test this out. I don't know exactly what is affecting you, but I put a sample on github so you can take a look and see if it helps you solve your problem.



https://github.com/jdecarlo/StackOverflow9920412

If the sample doesn't help solve your problem, please add a comment to this answer explaining why and I'll change it.

+2


source


Are you sure the autoresist masks are set correctly? To test this, use a breakpoint somewhere after all views and dungeons have been created and enter this command into the GDB window:

po [[self mainView] recursiveDescription]

      

it will show you the entire hierarchy of views and subplots with all their frames and autoresist masks.

+1


source







All Articles