简体   繁体   中英

Change the orientation only for specific view controllers in ios swift 2.3 and swift 3

I was searching that how can I change the orientation to LandScape mode only for spefic view controller when the Device Orientation is set to Portrait in the project settings . Didn't get any succesfull result. so finally I found a way to do it.

this is project device orientation looks like ....

在此处输入图片说明

In swift 2.3 add following methods

override func shouldAutorotate() -> Bool {
        return false
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.LandscapeLeft
    }


    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return UIInterfaceOrientation.LandscapeLeft
    }

For swift 3.0 do like below ....

  override var shouldAutorotate: Bool {
        return false
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscapeLeft
    }

    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .landscapeLeft
    }

Note :This will work fine, if you navigate from one view to another view controller without a navigation controller .

for more see this small video tutorial : change device orientation for specific view controller in ios

if anyone has better way to do this or comments , hope your answer or comments here . thanx .

Use supportedInterfaceOrientationsFor application delegate.add following code in AppDelegate file

 func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

            if isIpadLandscape {
                 return UIInterfaceOrientationMask.all;
            }else{
                return UIInterfaceOrientationMask.portrait;

            }


    }


Add particular **viewcontroller** page you want to **rotate**

     override func viewDidLoad() {
            super.viewDidLoad()
            isLandscape = true
    }
    override func viewWillDisappear(_ animated: Bool) {
            isLandscape = false
        }

And dont forget to add global varibale in appdelegate file

import UIKit
import CoreData
import Foundation
    var isLandscape = false
    @UIApplicationMain

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM