简体   繁体   中英

if app device orientation is portrait how to set force landscape only for one view constroller in ios, swift 2.3

for my project, device orientation is portrair . but only for one view controller when view did load , without rotating the phone, I want to rotate the view like 'landscape left`. I tried followings . in my view did load,

override func viewDidLoad() {
        super.viewDidLoad()

        let value = UIInterfaceOrientation.LandscapeLeft.rawValue
        UIDevice.currentDevice().setValue(value, forKey: "orientation")

    }

and I used this method ...

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

but nothing happned. then I use following method too with above methods.

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

then it cause for a crash. and nothing worked for me. any idea how to do that.

this is my project device orientation settings looks like ...

在此处输入图片说明

how can I do this. hope your help with this.

The best way to do what you want is to allow all Orientations in your app settings.

Then set the "should autorotate" variable to true il all your viewControllers

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

Then set the supportedInterfaceOrientations depending of your ViewContoller

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .Landscape

}

or

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

For example here I have two viewcontrollers, the view orientation will change when the function viewDidLoad will be called.

import UIKit

class PortraitViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //Here the viewController will appear in Portrait (Also all subclass of it)
    }

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

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

}

class LandscapeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //Here the viewController will appear in Landscape (Also all subclass of it)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Landscape

    }

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

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