Skywalker
Skywalker

Reputation: 5194

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'

Upvotes: 0

Views: 3833

Answers (2)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

Use this code:

override func viewDidAppear(animated: Bool) {
    let value = UIInterfaceOrientation.LandscapeLeft.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

And your Device orientation should be:

enter image description here

Result:

enter image description here

Sample for more Info.

UPDATE:

It is simple to lock orientation for particular view by adding this code:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask{

    return UIInterfaceOrientationMask.Portrait
}

Upvotes: 0

rshev
rshev

Reputation: 4176

Implement this in your View Controller:

override func viewDidLoad() {
    super.viewDidLoad()
    UIViewController.attemptRotationToDeviceOrientation()
}

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

settings info

Upvotes: 2

Related Questions