以编程方式更改方向无法正常工作 [英] Changing orientations programmatically does not work fine
问题描述
我的整个应用程序处于portrait
模式.我只想在landscape
模式下使用一个视图控制器(左).
My whole application is in portrait
mode. I just want to use one view controller in landscape
mode (left).
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask
{
if let _navigationController = window?.rootViewController as? UINavigationController {
if _navigationController.topViewController is FullScreenPlayerVC {
return UIInterfaceOrientationMask.LandscapeLeft
}
}
return UIInterfaceOrientationMask.Portrait
}
这是我的控制器A
override func shouldAutorotate() -> Bool
{
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask
{
return UIInterfaceOrientationMask.Portrait
}
现在我按下控制器B.这是我的控制器B
Now i push Controller B. This is my controller B
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
override func shouldAutorotate() -> Bool
{
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask
{
return UIInterfaceOrientationMask.Landscape
}
当我将控制器B保持在纵向模式时按控制器B时,它按我的要求工作,但是如果我已经将手机保持在横向模式中,则可以按我的要求进行操作.
It works as per my requirement when i push controller B while holding my device in portrait mode but if i am holding my phone in landscape left already.
它没有执行所需的操作.搜索了很多关于它,但是还找不到解决方案.
It does not perform desired action. Searched a lot about it but not able to find the solution yet.
我尝试了许多解决方案,但没有任何效果.
I have tried many solutions but nothings working fine.
推荐答案
这是针对您的问题和其他相关问题的通用解决方案.
This is a generic solution for your problem and others related.
1.创建辅助类UIHelper并使用以下方法:
/**This method returns top view controller in application */
class func topViewController() -> UIViewController?
{
let helper = UIHelper()
return helper.topViewControllerWithRootViewController(rootViewController: UIApplication.shared.keyWindow?.rootViewController)
}
/**This is a recursive method to select the top View Controller in a app, either with TabBarController or not */
private func topViewControllerWithRootViewController(rootViewController:UIViewController?) -> UIViewController?
{
if(rootViewController != nil)
{
// UITabBarController
if let tabBarController = rootViewController as? UITabBarController,
let selectedViewController = tabBarController.selectedViewController {
return self.topViewControllerWithRootViewController(rootViewController: selectedViewController)
}
// UINavigationController
if let navigationController = rootViewController as? UINavigationController ,let visibleViewController = navigationController.visibleViewController {
return self.topViewControllerWithRootViewController(rootViewController: visibleViewController)
}
if ((rootViewController!.presentedViewController) != nil) {
let presentedViewController = rootViewController!.presentedViewController;
return self.topViewControllerWithRootViewController(rootViewController: presentedViewController!);
}else
{
return rootViewController
}
}
return nil
}
2.根据您的期望行为创建协议,因为您的具体情况将是横向的.
协议的方向是IsOnlyLandscape {}
protocol orientationIsOnlyLandscape {}
注意:如果需要,可将其添加到UIHelper类的顶部.
Nota: If you want, add it in the top of UIHelper Class.
3.扩展您的View Controller
在您的情况下:
class B_ViewController: UIViewController,orientationIsOnlyLandscape {
....
}
4.在应用程序委托类中,添加以下方法:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
let presentedViewController = UIHelper.topViewController()
if presentedViewController is orientationIsOnlyLandscape {
return .landscape
}
return .portrait
}
最后的注释:
- 如果您有更多的课程是横向模式,只需扩展 协议.
- 如果您希望视图控制器具有其他行为,请创建其他协议并遵循相同的结构.
- 此示例解决了推后方向改变的问题 查看控制器
- If you that more class are in landscape mode, just extend that protocol.
- If you want others behaviors from view controllers, create other protocols and follow the same structure.
- This example solves the problem with orientations changes after push view controllers
这篇关于以编程方式更改方向无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!