以编程方式更改方向不起作用
Changing orientations programmatically does not work fine
我的整个应用程序处于 portrait
模式。我只想在 landscape
模式下使用一个视图控制器(左)。
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
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 时,它会按照我的要求工作,但如果我已经将 phone 横向握住。
它没有执行所需的操作。搜索了很多关于它但无法找到解决方案。
我已经尝试了很多解决方案,但没有任何效果。
这是针对您的问题和其他相关问题的通用解决方案。
1.创建辅助 class 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。根据您的期望行为创建一个协议,您的具体情况将是横向的。
协议 orientationIsOnlyLandscape {}
注意:如果需要,请将其添加到 UIHelper 的顶部 Class。
3。扩展你的视图控制器
你的情况:
class B_ViewController: UIViewController,orientationIsOnlyLandscape {
....
}
4.在应用程序委托 class 中添加此方法:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
let presentedViewController = UIHelper.topViewController()
if presentedViewController is orientationIsOnlyLandscape {
return .landscape
}
return .portrait
}
最后的笔记:
- 如果您有更多 class 处于横屏模式,请将其扩展
协议。
- 如果您想要视图控制器的其他行为,请创建其他协议并遵循相同的结构。
- 这个例子解决了推送后方向改变的问题
视图控制器
我的整个应用程序处于 portrait
模式。我只想在 landscape
模式下使用一个视图控制器(左)。
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
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 时,它会按照我的要求工作,但如果我已经将 phone 横向握住。
它没有执行所需的操作。搜索了很多关于它但无法找到解决方案。
我已经尝试了很多解决方案,但没有任何效果。
这是针对您的问题和其他相关问题的通用解决方案。
1.创建辅助 class 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。根据您的期望行为创建一个协议,您的具体情况将是横向的。
协议 orientationIsOnlyLandscape {}
注意:如果需要,请将其添加到 UIHelper 的顶部 Class。
3。扩展你的视图控制器
你的情况:
class B_ViewController: UIViewController,orientationIsOnlyLandscape {
....
}
4.在应用程序委托 class 中添加此方法:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
let presentedViewController = UIHelper.topViewController()
if presentedViewController is orientationIsOnlyLandscape {
return .landscape
}
return .portrait
}
最后的笔记:
- 如果您有更多 class 处于横屏模式,请将其扩展 协议。
- 如果您想要视图控制器的其他行为,请创建其他协议并遵循相同的结构。
- 这个例子解决了推送后方向改变的问题 视图控制器