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 !
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.
I usually solve this by taking advantage of the
autoresizing
techniques in the view combined with the implementation ofwillAutorotateToInterfaceOrientation
andwillAnimateRotationToInterfaceOrientation
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 toYES
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 theautoresizingMask = 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!