简体   繁体   English

iOS:如何更改应用程序中仅一个视图控制器的方向?

[英]iOS: How to change Orientation of only one view controller in app?

I am facing problem in view orientation in my app. 我在应用程序中的视图方向遇到问题。

Like 喜欢

I have two view controller, VC1 and VC2 我有两个视图控制器,VC1和VC2

VC1 have fix landscape orientation. VC1具有固定的横向方向。 VC2 have both VC2兼有

VC1 -> VC2 is fine. VC1 - > VC2很好。 means when I go from VC1 to VC2, VC2 change its orientation in both landscape and portrait. 这意味着当我从VC1转到VC2时,VC2会同时改变其横向和纵向方向。

But when I comeback to VC1 from VC2(where VC2 in portrait mode), VC1 also is in portrait mode but I want VC1 is in landscape only irrespective of VC2 mode. 但是,当我从VC2(其中VC2为纵向模式)恢复到VC1时,VC1也处于纵向模式,但是我希望VC1仅处于横向模式,而与VC2模式无关。

Please guys help me. 请大家帮帮我 Seeking solution from last 2 days. 寻求最近2天的解决方案。 Thanks in advance. 提前致谢。

Refer below link for solution 请参阅下面的链接以获取解

http://swiftiostutorials.com/ios-orientations-landscape-orientation-one-view-controller/ http://swiftiostutorials.com/ios-orientations-landscape-orientation-one-view-controller/

In your VC1.. 在您的VC1中。

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

Hope it helps you... 希望它能帮到你......

First of all, write this in AppDelegate.m 首先,在AppDelegate.m中编写

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
 return (UIInterfaceOrientationMaskAll);
}

Then, For VC1, in which landscape orientation, write this: 然后,对于VC1(横向),编写以下代码:

- (BOOL)shouldAutorotate
{
 return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
 return (UIInterfaceOrientationMaskPortrait);
}

For VC2, which have both, change masking to All. 对于同时具有两者的VC2,请将掩码更改为全部。

- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskAllButUpsideDown);
//OR return (UIInterfaceOrientationMaskAll);
}

basically the view-controller-based orientation support in an iOS app is a piece of cake – I did the code in Swift , but ObjC concept would be totally identical to this. 基本上iOS应用程序中基于视图控制器的方向支持是小菜一碟 - 我在Swift中完成了代码,但是ObjC概念与此完全相同。

I have created a quick tutorial with UINavigationController but you can convert that to UITabBarController by keeping the concept but updating the environment slightly. 我已经使用UINavigationController创建了一个快速教程,但您可以通过保留概念但稍微更新环境将其转换为UITabBarController

step-1 第1步

initially set the orientation support for your app like this: 最初为您的应用设置方向支持,如下所示:

默认情况下支持所有可能的方向

step-2 第2步

create a simple UINavigationController -based storyboard like this: 创建一个基于UINavigationController的简单故事板,如下所示:

这就是最终的结果

NOTE: don't worry if it does not say initially OrientaionSupporterController (bonus point if you'd spot the consistent typo here!), I will cover that just in the very next step. 注意: 如果它最初没有说OrientaionSupporterController请不要担心(如果你在这里发现一致错误,则OrientaionSupporterController奖励!),我将在下一步中介绍它。

step-3 第3步

create a subset of the navigation-controller, like this: 创建导航控制器的子集,如下所示:

import UIKit

// MARK: - Implementation

class OrientaionSupporterController: UINavigationController {

    // MARK: - Active View Controller

    private var activeViewController: UIViewController? {
        get {
            return self.presentedViewController ?? self.topViewController // for possible modal support
        }
    }

    // MARK: - Orientations

    override var shouldAutorotate: Bool {
        get {
            return self.activeViewController?.shouldAutorotate ?? true // yes, rotate it, please
        }
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return self.activeViewController?.supportedInterfaceOrientations ?? .all // default is all possible orientations
    }

}

then make sure the that becomes the base class in IB as well: 然后确保它成为IB中的基类:

从现在开始,这是导航控制器的基类

step-3 第3步

create the individual view-controllers with custom orientation support, like eg with only portrait and landscape-left support: 创建具有自定义方向支持的各个视图控制器,例如仅支持纵向横向左支持:

import UIKit

// MARK: - Implementation

class ViewController: UIViewController {

    // MARK: - Orientation

    override var shouldAutorotate: Bool {
        get {
            return true
        }
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return [.portrait, .landscapeLeft] // no one find that preactical but works flawlessly
    }

}

NOTE: you can convert this tutorial anytime with UITabBarController , you just need to (obviously) create a subset of the tab-bar-controller and use the selectedViewController to get the currently visible one. 注意: 您可以随时使用UITabBarController转换本教程,您只需(显然)创建tab-bar-controller的子集并使用selectedViewController获取当前可见的一个。


NOTE#2: obviously you can go much further on this way and you can nest the customised navigation-controllers in the view hierarchy, or if you have multiple UIWindow instances in the queue and override the supported orientations there as well for each individual windows (eg some support all four orientations, while some other windows does only landscape for eg video playback, etc...) 注意#2: 很明显,您可以通过这种方式更进一步,您可以在视图层次结构中嵌套自定义导航控制器,或者如果队列中有多个UIWindow实例,并覆盖每个窗口中支持的方向(例如,一些支持所有四个方向,而一些其他窗口仅支持例如视频播放,等等...)

1. Enable all orientation support for project. 1.启用项目的所有方向支持。

For VC1 Add this line VC1Controller Class 对于VC1,添加此行VC1Controller类

- (UIInterfaceOrientationMask)supportedInterfaceOrientations { 
  return UIInterfaceOrientationMaskLandscape;
}

For VC2 Add this in VC2Controller Class 对于VC2在VC2Controller类中添加它

- (UIInterfaceOrientationMask)supportedInterfaceOrientations { 
      return UIInterfaceOrientationMaskAll;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM