Bao Tuan Diep Bao Tuan Diep - 3 months ago 10
Swift Question

iOS. How can enable or disable rotate on one UIViewController

I have one UIViewController, I want to rotate or not rotate the screen in different cases

Example:

if flag {
rotateDevice = false
}
else {
rotateDevice = true
}


How can I do that?

Answer

I have the answer. On AppDelegate, if you rotate device, push viewcontroller,... This function alway call

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask
{
    if self.restrictRotation {
        return UIInterfaceOrientationMask.All
    }
    else {
        return UIInterfaceOrientationMask.Landscape
    }
}

Make restrictRotation type is static and change it if you want to your custom orientation device

Comments