首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS:如何在app中只更改一个视图控制器的方向?

iOS:如何在app中只更改一个视图控制器的方向?
EN

Stack Overflow用户
提问于 2015-02-11 16:04:12
回答 4查看 1.6K关注 0票数 0

我在我的应用程序中遇到了视图方向的问题。

喜欢

我有两个视图控制器,VC1和VC2

VC1具有固定的横向。VC2两者兼备

VC1 -> VC2很好。意思是当我从VC1转到VC2时,VC2在横向和纵向上都会改变它的方向。

但当我从VC2 ( VC2处于纵向模式)返回VC1时,VC1也处于纵向模式,但我希望VC1仅处于横向模式,而不考虑VC2模式。

请各位帮帮我。从过去2天开始寻求解决方案。提前谢谢。

EN

回答 4

Stack Overflow用户

发布于 2015-02-11 18:19:12

有关解决方案,请参阅以下链接

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

在你的VC1中..

代码语言:javascript
复制
-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

希望它能帮到你。

票数 0
EN

Stack Overflow用户

发布于 2017-12-21 18:06:05

首先,在AppDelegate.m中编写以下代码

代码语言:javascript
复制
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
 return (UIInterfaceOrientationMaskAll);
}

然后,对于横向方向的VC1,写下:

代码语言:javascript
复制
- (BOOL)shouldAutorotate
{
 return YES;
}

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

对于同时拥有两者的VC2,请将掩码更改为All。

代码语言:javascript
复制
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskAllButUpsideDown);
//OR return (UIInterfaceOrientationMaskAll);
}
票数 0
EN

Stack Overflow用户

发布于 2017-12-21 19:10:20

基本上,在iOS应用程序中支持基于视图控制器的方向是小菜一碟--我用Swift编写了代码,但ObjC的概念与此完全相同。

我已经用UINavigationController创建了一个快速教程,但您可以通过保留概念但稍微更新环境来将其转换为UITabBarController

第1步

最初设置应用程序的方向支持如下所示:

第2步

创建一个简单的UINavigationController-based脚本,如下所示:

备注:如果没有显示初始OrientaionSupporterController (如果你能在这里发现一致的拼写错误,你就会得到加分),请不要担心,我将在下一步讨论这个问题。

第3步

创建导航控制器的子集,如下所示:

代码语言:javascript
复制
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
    }

}

然后确保它也成为IB中的基类:

第3步

创建具有自定义方向支持的单个视图控制器,例如,仅支持纵向和左侧横向:

代码语言:javascript
复制
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
    }

}

注意:您可以随时使用UITabBarController__转换本教程,您只需要(显然)创建选项卡栏控制器的一个子集,并使用selectedViewController获取当前可见的子集。

NOTE#2:显然你可以在这种方式上走得更远,你可以在视图层次结构中嵌套定制的导航控制器,或者如果你在队列中有多个UIWindow实例,并覆盖每个单独窗口的支持方向(例如,一些窗口支持所有四种方向,而另一些窗口仅用于视频播放等)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28449316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档