xcode - Alternative iOS layouts for portrait and landscape using just one .xib file -
using interface builder in xcode , 1 .xib file, how can create alternate layouts when rotating between landscape , portrait orientations?
see diagram of differing layouts
n.b. green view/area contain 3 items flowing horizontal in landscape , in portrait 3 items flow vertically within green view/area.
a way have 3 views in .xib file. first 1 normal view of viewcontroller no subviews.
then create views portrait , landscape need them. 3 have root-level views (have @ screenshot)
in viewcontroller, create 2 iboutlets, 1 portrait , 1 landscape view , connect them corresponding views in interface builder:
iboutlet uiview *_portraitview; iboutlet uiview *_landscapeview; uiview *_currentview;
the third view, _currentview
needed keep track of 1 of these views being displayed. create new function this:
-(void)setupviewfororientation:(uiinterfaceorientation)orientation { [_currentview removefromsuperview]; if(uiinterfaceorientationislandscape(orientation)) { [self.view addsubview:_landscapeview]; _currentview = _landscapeview; } else { [self.view addsubview:_portraitview]; _currentview = _portraitview; } }
you need call function 2 different places, first initialization:
-(void)viewdidload { [super viewdidload]; uiinterfaceorientation interfaceorientation = [[uiapplication sharedapplication] statusbarorientation]; [self setupviewfororientation:interfaceorientation]; }
and second orientation changes:
-(void)willrotatetointerfaceorientation:(uiinterfaceorientation)tointerfaceorientation duration:(nstimeinterval)duration { [self setupviewfororientation:tointerfaceorientation]; }
hope helps you!
Comments
Post a Comment