I have one UIViewController which manage a portrait view and some application logic. However, I need to setup a different view for the landscape orientation. The widgets between the 2 views are mainly the same but their layout is very different.
My first try was to use one UIViewController and one associated XIB. In the XIB, I have two UIView, a LandscapeView for landscape, and a PortraitView for portrait.
myController.xib
|myController (File's Owner) |landscapeView |-- myWidget |-- ... |portraitView |-- myWidget |-- ...Then myController switch between these views depending on the screen orientation by setting its view property.
In my example, the myWidget are essentially the same between the two views, only their positioning change. The controller has an IBOutlet to one of those, and this IBOutlet is changed programmat开发者_StackOverflow中文版ically when a orientation change occurs, to point to the correct one.
Is there a better solution?
I checked the AlternateViews sample code from Apple but they use 2 UIViewController for 2 different views. I would like to keep all event/delegate handling code in only one controller, not to split it between 2 controllers.
You likely have another UIView
in the hierarchy, the one that gets assigned to the view controller's view
property when it is created/loaded, to which you have added landscapeView
and portraitView
. If this is the case, then you could add myWidget to that same parent view, and so long as it is last in the subviews
array, it will show up on top as you show/hide the other two views. If you don't already have that parent view, and are managing the view
property manually, then you could add one to be the parent.
精彩评论