Search code examples
iosswiftuiwebviewswift2landscape

Force landscape orientation for UIWebView?


I have an app where all the views are portraits but only one view which is a UIWebView which loads a PDF needs to be in Landscape.

My project settings are below:

enter image description here

I have tried the following:

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

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    let orientation: UIInterfaceOrientationMask = [UIInterfaceOrientationMask.Landscape, UIInterfaceOrientationMask.LandscapeLeft]
    return orientation
}

The above code does not seem to be working. I got the code by searching online. I am not sure what I am doing wrong. Any suggestions would be help and if you can provide some sample code would be great.

I just want to force landscape orientation for one UIWebView.

EDIT:

enter image description here

Error:

enter image description here

If its hard to read here is the error:

Supported orientations has no common orientation with the application, and [KM_T_World.MainViewController shouldAutorotate] is returning YES'


Solution

  • Implement this in your View Controller:

    override func viewDidLoad() {
        super.viewDidLoad()
        UIViewController.attemptRotationToDeviceOrientation()
    }
    
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return [UIInterfaceOrientationMask.LandscapeLeft, UIInterfaceOrientationMask.LandscapeRight]
    }
    

    settings info