ios - How to change viewController based on device orientation -
for last few days i've been trying figure out way this:
change collectionviewcontroller if in landscape mode. change viewcontroller if in portrait mode.
pretty way music app behaves.
the way did implement viewwilltransitiontosize() in main portraitviewcontroller class:
override func viewwilltransitiontosize(size: cgsize, withtransitioncoordinator coordinator: uiviewcontrollertransitioncoordinator) { let storyboard = uistoryboard(name: "main", bundle: nsbundle.mainbundle()) let portraitviewcontroller = storyboard.instantiateviewcontrollerwithidentifier("portraitviewcontroller") as! viewcontroller let landscapeviewcontroller = storyboard.instantiateviewcontrollerwithidentifier("landscapeviewcontroller") as! beercollectionviewcontroller if (uidevice.currentdevice().orientation.islandscape) { // in landscape presentviewcontroller(landscapeviewcontroller, animated: true, completion: nil) } else { // in portrait self.navigationcontroller?.presentviewcontroller(portraitviewcontroller, animated: true, completion: nil) //let navvc = uinavigationcontroller(rootviewcontroller: portraitviewcontroller) //presentviewcontroller(navvc, animated: true, completion: nil) //dismiss collectionviewcontroller after switching portrait self.dismissviewcontrolleranimated(true, completion: nil) } }
main viewcontroller(portraitviewcontroller) contains tableview , searchcontroller has searchbar on top.
in storyboard, portraitviewcontroller embedded in navigation controller, collectionviewcontroller(landscapeviewcontroller) on own.
it runs, following 2 warnings:
attempt present uinavigationcontroller on viewcontroller view not in window hierarchy!
attempting load view of view controller while deallocating not allowed , may result in undefined behavior (uisearchcontroller)
can point out what's going wrong here? if there's tutorial/examples can of appreciate it! thank help
edit: here final code works
override func viewwilltransitiontosize(size: cgsize, withtransitioncoordinator coordinator: uiviewcontrollertransitioncoordinator) { let storyboard = uistoryboard(name: "main", bundle: nsbundle.mainbundle()) // in landscape if uideviceorientationislandscape(uidevice.currentdevice().orientation) { let landscapevc = storyboard.instantiateviewcontrollerwithidentifier("landscapeviewcontroller") as! beercollectionviewcontroller if presentedviewcontroller != nil { if !presentedviewcontroller!.isbeingdismissed() { self.dismissviewcontrolleranimated(false, completion: { self.presentviewcontroller(landscapevc, animated: true, completion: nil) }) } } else { self.presentviewcontroller(landscapevc, animated: true, completion: nil) } } // in portrait else { let navc = storyboard.instantiateviewcontrollerwithidentifier("navid") as! uinavigationcontroller let portraitvc = storyboard.instantiateviewcontrollerwithidentifier("portraitviewcontroller") as! viewcontroller if presentedviewcontroller != nil { if !presentedviewcontroller!.isbeingdismissed() { self.dismissviewcontrolleranimated(false, completion: { navc.pushviewcontroller(portraitvc, animated: true) }) } } else { navc.pushviewcontroller(portraitvc, animated: true) } }
the reason warnings or errors there delay when dismissing , presenting view controllers , calls present , dismiss overlapping each other.
the first error reporting view not in hierarchy means attempting present view on original view controller or navigation controller no longer available because view controller has been modally presented.
the second error regarding loading view while deallocating indicates view controller dismissal has not completed when modal presentation being attempted.
to prevent these problems requires ensuring dismissals complete before presenting new view controller. can accomplished handling states using completion handler in dismiss methods , adding check on isbeingdismissed
state.
here code accomplishes that:
override func viewwilltransitiontosize(size: cgsize, withtransitioncoordinator coordinator: uiviewcontrollertransitioncoordinator) { super.viewwilltransitiontosize(size, withtransitioncoordinator: coordinator) let storyboard = uistoryboard(name: "main", bundle: nil) if uideviceorientationislandscape(uidevice.currentdevice().orientation) { let landscapevc = storyboard.instantiateviewcontrollerwithidentifier("landscapeviewcontroller") as! beercollectionviewcontroller if presentedviewcontroller != nil { if !presentedviewcontroller!.isbeingdismissed() { dismissviewcontrolleranimated(false, completion: { self.presentviewcontroller(landscapevc!, animated: true, completion: nil) }) } } else { self.presentviewcontroller(landscapevc!, animated: true, completion: nil) } } else { // portrait let portraitvc = storyboard.instantiateviewcontrollerwithidentifier("portraitviewcontroller") as! viewcontroller if presentedviewcontroller != nil { if !presentedviewcontroller!.isbeingdismissed() { dismissviewcontrolleranimated(false, completion: { self.presentviewcontroller(portraitvc!, animated: true, completion: nil) }) } } else { self.presentviewcontroller(portraitvc!, animated: true, completion: nil) } } }
Comments
Post a Comment