iPhone orientation management : what is the most e

2019-06-07 04:41发布

I need to develop an iPad application which should manage the two orientation mode (landscape and portrait). According the official Apple iOS documentation, there are 2 ways to proceed.

-The first one consists in adjusting views element when the rotation event is received. The main advantage is that we had only one ViewController -The second one consists in displaying a specific ViewController for each orientation. Therefore, we have 2 ViewControllers.

The second approach seems to be nice, but I'am afraid by the numbers of ViewController that will be needed. What's more, the "data synchronisation logic" in the ViewControllers will have to be duplicated (or isolated from the ViewController) to be used in both orientation.

The application I need to develop will contain many "full custom elements" and many ViewControllers.

If anyone has advices or experience feedback, it would be really appreciated ;)

Thank's for reading !

2条回答
神经病院院长
2楼-- · 2019-06-07 05:23

The second way should rather be: using 2 different views (one for portrait, one for landscape) and swapping the view controller's view in willRotateToInterfaceOrientation:. No need to duplicate your data logic.

Which way to use? I would say: it depends.

  • If the lanscape and the portrait modes differ only by the position / size of views, I use the first one (plus you'll get nice animations of the frame changes)
  • If landscape and portrait are too different, I prefer the second one.
查看更多
狗以群分
3楼-- · 2019-06-07 05:29

I usually solve this by taking advantage of the autoresizing techniques in the view combined with the implementation of willAutorotateToInterfaceOrientation and willAnimateRotationToInterfaceOrientation methods in the view controller.

With autoresizing techniques you can easily resize standard UI elements provided by Apple. If your UI elements doesn't have an impossible layout, you can apply the autoresizing techniques to them too. You must set the autoresizesSubviews property to YES in the parent view controller and select an autoresizing behaviour for each subview. For example, if you want it to resize to the right maintaining the view centered, you can apply the autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin mask in the subview.

If the autoresizing techniques doesn't do the trick, then you will need to resize each conflicting view separately by implementing the - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration in your view controller. If you have "full custom elements", you will need to resize them this way.

In my particular experience, I prefer to have only one view controller and one view for all orientations and manage them with these two techniques.

Hope this helps you!

查看更多
登录 后发表回答