简体   繁体   中英

ios Handling of parentViews if orientation changes

Maybe a basic problem, it's my first app dealing with different orientations on iPad.

UIView1 --> UIButton --> Presents UIView2

In UIView1 and UIView2 i set the frame and Size of containing Views depending on orientation in

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

This is ok. But if View2 is in focus, the orientation changes and i close View2, View1 did not respond to orientation change. The View itself is in correct orientation, but the calculation of frame and size of in UIView1 contained elements is not executed.

What is the "correct" way to do this? Do i have to call a delegate method in View2 to set the new positions in View1 (hopefully resisting in background if it was not released because of memory)?

MadMaxApp

You could implement

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                            duration:(NSTimeInterval)duration;

or

 willAnimateRotationToInterfaceOrientation:duration

in your view controllers.

Those methods will be called before the rotation changes and gives you a possibility of manually handling the rotation. This can be useful if the autoresize is not giving the correct results (I understand you are doing some custom calculations about your views layout in shouldAutorotateToInterfaceOrientation , so it might be the case).

On a more general note, consider that there are several methods that deal with rotation; they have different responsibilities:

  1. shouldAutorotateToInterfaceOrientation : it just says which orientation are supported;

  2. willAnimateRotationToInterfaceOrientation:duration : this is the place where you can modify frames, sizes, positions, etc. when you want that those changes take effect during while the rotation is occurring;

  3. willRotateToInterfaceOrientation:duration: : do here whatever is necessary: stopping animations, timers, live update and so on;

  4. didRotateFromInterfaceOrientation: : do here the opposite to 3.

This is just a guideline, of course. You could

The answer depends on where you're telling your interface pieces to position themselves. It could be as simple as giving them all the right autoresizing mask values in code or in the nib editor. Or you might have to implement didRotateFromInterfaceOrientation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM