0

I have a table and each one is assigned a link. When a row is tapped the link opens in a KINWebBrowserViewController view. I don't want the table to allow landscape but I want the web browser to allow it. How can I do it?

Some Code:

    let linkItem = linkLabels[indexPath.row] as String

    let webBrowser = KINWebBrowserViewController()
    let url = NSURL(string: linkItem)
    webBrowser.actionButtonHidden = true
    webBrowser.loadURL(url)

    self.navigationController?.pushViewController(webBrowser, animated: true)

All suggestions welcomed. Thanks :)

Nathan Schafer
  • 273
  • 4
  • 15

1 Answers1

1

In the General - deployment info section of your project in Xcode, tick all the permitted orientations for any screen in your app.

iOS calls the supportedInterfaceOrientations method in the rootmost UIViewController that is visible on the screen. If a VC covers the whole screen, then earlier VCs (further towards the root viewController) do not have that method called.

If your webBrowser VC covers the whole screen, then put a method in that:

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

If webBrowser does not cover the whole screen, and navigationController is still showing a navigation bar, then that method will not be called, and the one in navigationController will be called instead. So, subclass UINavigationController:

class CustomNC:UINavigationController {
    override func supportedInterfaceOrientations() -> Int { 
        if visibleViewController is KINWebBrowserViewController { return Int(UIInterfaceOrientationMask.All.rawValue) }
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    }
}

and declare your UINavigationController to be a CustomNC instead. I haven't tested the code, it may have typos, but that technique is working for me now.

More explanation: CustomNC is a new, cleverer UINavigationController that you are just going to invent. You can put the code for that in any swift file in your project. It is just like an existing UINavigationController, except it changes behaviour on rotation. So you want it to inherit all the behaviour of UINavigationController, and then just override the rotation bit. This bit does the inheriting:

class CustomNC:UINavigationController {

and this bit does the overriding:

override func supportedInterfaceOrientations() -> Int { 

And you need to tell Xcode that your Root ViewController is a CustomNC rather than a plain UINavigationController. If you've built the project with a storyboard, you can click the UINavigationController to select it, then change its class from UINavigationController to CustomNC in the Identity Inspector tab at the top right of Xcode.

emrys57
  • 6,679
  • 3
  • 39
  • 49