ios - How to lock orientation just for one view controller? -
can set portrait orientation , lock orientation in 1 controller?
i did try:
override func supportedinterfaceorientations() -> int { return int(uiinterfaceorientationmask.portrait.rawvalue) } override func shouldautorotate() -> bool{ return false }
but doesn't me. must add else?
this code should work:
override func supportedinterfaceorientations() -> int { return int(uiinterfaceorientationmask.portrait.rawvalue) } override func shouldautorotate() -> bool{ return false } override func preferredinterfaceorientationforpresentation() -> uiinterfaceorientation { return uiinterfaceorientation.portrait }
if working you, suppose controller in controller(uinavigationcontroller
, uitabbarcontroller
, uisplitviewcontroller
). in case need use code in parent controller.
if navigation controller contain more 1 view controller, , need disable orientation of them, need inherit uinavigationcontroller
class , write there like:
class navigationcontroller: uinavigationcontroller { var shouldrotate: bool = true override func supportedinterfaceorientations() -> int { return shouldrotate ? int(uiinterfaceorientationmask.portrait.rawvalue) : int(uiinterfaceorientationmask.all.rawvalue) } override func shouldautorotate() -> bool{ return shouldrotate } }
then in controller need disable orientation can disable navigation controller:
class viewcontroller: uiviewcontroller { var lastcontrollerrotationstatus: bool? override func viewwillappear(animated: bool) { super.viewwillappear(animated) if let navigationcontroller = self.navigationcontroller as? navigationcontroller { lastcontrollerrotationstatus = navigationcontroller.shouldrotate navigationcontroller.shouldrotate = false } } override func viewdiddisappear(animated: bool) { super.viewdiddisappear(animated) if let navigationcontroller = self.navigationcontroller as? navigationcontroller { navigationcontroller.shouldrotate = lastcontrollerrotationstatus ?? true } } }
but remember, need restore old rotation status after controller pushed out navigation controller. in example i'm saving rotation status before changing it, , restoring after controller disappeared. can use other approach. example can overload uinavigationcontroller
method popviewcontroller
, , there set shouldrotate
false.
the same approach variable setting controller can use controlling rotation appdelegate
method application(application: uiapplication, supportedinterfaceorientationsforwindow window: uiwindow) -> int
then not need inherit navigation controller.
your appdelegate
class code like:
class appdelegate: uiresponder, uiapplicationdelegate { var window: uiwindow? var shouldrotate = true func application(application: uiapplication, supportedinterfaceorientationsforwindow window: uiwindow?) -> int { return shouldrotate ? int(uiinterfaceorientationmask.all.rawvalue) : int(uiinterfaceorientationmask.portrait.rawvalue) } }
and controller code like:
class viewcontroller: uiviewcontroller { var lastcontrollerrotationstatus: bool? override func viewwillappear(animated: bool) { super.viewwillappear(animated) if let appdelegate = uiapplication.sharedapplication().delegate as? appdelegate { lastcontrollerrotationstatus = appdelegate.shouldrotate appdelegate.shouldrotate = false } } override func viewdiddisappear(animated: bool) { super.viewdiddisappear(animated) if let appdelegate = uiapplication.sharedapplication().delegate as? appdelegate { appdelegate.shouldrotate = lastcontrollerrotationstatus ?? true } } }
Comments
Post a Comment