Reputation: 197
am developing SDK for video player. But the problem is video auto rotation. When the app supports for both portrait and land scape it works fine. When app supports only portrait app then the auto rotation fails. How do i do the forceful .all orientation from SDK side.
One solution is by implementing func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask in AppDelegate it will work.
One more solution is overriding shouldAutorotate(), supportedInterfaceOrientations() in my VideoViewController this works only when app too supports both(PORTRAIT and LANDSCAPE) orientation.
But in my case SDK needs to be handle the orientation, because am presenting the VideoViewController above any visible controller. and am not exposing my VideoViewController to app.
How can i achieve it.. Any solution.
Upvotes: 0
Views: 183
Reputation: 89
If your project is already in portrait you won’t need to change anything. If not, make sure that only portrait is selected.
In Your AppDelegate add this:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
if let vcp = rootViewController as? ViewControllerRotateProtocol, vcp.canRotate() {
// Unlock landscape view orientations for this view controller
return [.landscapeLeft , .landscapeRight]
}
}
return application.supportedInterfaceOrientations(for: window)
}
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
}
Create one protocol: and you can expose this protocol to the app.
protocol ViewControllerRotateProtocol {
func canRotate() -> Bool
}
In Your View Controller, add this code:
class ViewController: UIViewController, ViewControllerRotateProtocol {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillDisappear(_ animated : Bool) {
super.viewWillDisappear(animated)
if (self.isMovingFromParentViewController) {
UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
}
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return [.landscapeLeft , .landscapeRight]
}
func canRotate() -> Bool {
return true
} }
Upvotes: 0