Swift: App in portrait mode - How can I set landscape mode only for one Controller/View/Page?

Hi there,

I use SafariServices to show a webpage. So the challenge is to use the landscape mode only with the SafariController.

Code Block
import SafariServices
let config = SFSafariViewController.Configuration()
config.entersReaderIfAvailable = true
config.barCollapsingEnabled = false
let vc = SFSafariViewController(url: url, configuration: config)
present(vc, animated: true)

In the project settings the "Device Orientation" is set to "Portrait".

Do someone know a solution for this?
Answered by LuminiCode in 636802022
I've developed the following solution:
  1. The Device Orientation in the Deployment Info in the settings is not set (no checkbox is checked).

  2. AppDelegate: the one controller in this case is "SFSafariViewController"

Code Block
//rotation 1
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
print(rootViewController)
if (rootViewController is SFSafariViewController) {
// Unlock landscape view orientations for this view controller
print("unlock")
return .allButUpsideDown;
}
}
//Only allow portrait (standard behaviour)
return .portrait;
}
//rotation 2
private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
if (rootViewController == nil) { return nil }
if (rootViewController.isKind(of: UITabBarController.self)) {
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)} else if (rootViewController.isKind(of: UINavigationController.self)) {
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
} else if (rootViewController.presentedViewController != nil) {
return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
}
return rootViewController
}


3. Programmatically created SFSafariViewController in the ViewController:

Code Block
import SafariServices
let url = URL(string : file_url)!
let config = SFSafariViewController.Configuration()
config.entersReaderIfAvailable = true
config.barCollapsingEnabled = false
let vc = SFSafariViewController(url: url, configuration: config)
present(vc, animated: true)


Accepted Answer
I've developed the following solution:
  1. The Device Orientation in the Deployment Info in the settings is not set (no checkbox is checked).

  2. AppDelegate: the one controller in this case is "SFSafariViewController"

Code Block
//rotation 1
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
print(rootViewController)
if (rootViewController is SFSafariViewController) {
// Unlock landscape view orientations for this view controller
print("unlock")
return .allButUpsideDown;
}
}
//Only allow portrait (standard behaviour)
return .portrait;
}
//rotation 2
private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
if (rootViewController == nil) { return nil }
if (rootViewController.isKind(of: UITabBarController.self)) {
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)} else if (rootViewController.isKind(of: UINavigationController.self)) {
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
} else if (rootViewController.presentedViewController != nil) {
return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
}
return rootViewController
}


3. Programmatically created SFSafariViewController in the ViewController:

Code Block
import SafariServices
let url = URL(string : file_url)!
let config = SFSafariViewController.Configuration()
config.entersReaderIfAvailable = true
config.barCollapsingEnabled = false
let vc = SFSafariViewController(url: url, configuration: config)
present(vc, animated: true)


Swift: App in portrait mode - How can I set landscape mode only for one Controller/View/Page?
 
 
Q