使UiViewController保持纵向模式iOS6 VS iOS5 [英] Make a UiViewController stay in portrait mode iOS6 VS iOS5

查看:100
本文介绍了使UiViewController保持纵向模式iOS6 VS iOS5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iOS5和iOS6构建应用程序。我在UINavigationController中有这个UIViewController,我希望它保持纵向模式。

I'm building an app for iOS5 and iOS6. I have this UIViewController inside a UINavigationController and I want it to stay in portrait mode.

该代码适用于iOS5,但不适用于iOS6。

The code works for iOS5, but not in iOS6.

// iOS5 rotation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if (interfaceOrientation == UIInterfaceOrientationPortrait)
        return YES;
    else
        return NO;
}

// iOS6 rotation
- (BOOL)shouldAutorotate
{

    return YES;


}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;

}

这里有什么问题?在SO上我发现了许多类似的问题,但一般来说答案对我不起作用。

What's the problem here??? On SO I've found lot's of similar questions, but generally the answers are not working for me.

编辑:
也许我不准确,但我需要一个单一视图控制器(我的应用程序的主页)保持纵向模式而不是所有的应用程序

推荐答案

首先,很大程度上取决于你的UIViewController嵌入哪个控制器。

First of all, A lot depends on with which controller is your UIViewController embedded in.

例如,如果它在UINavigationController里面,那么你可能需要继承UINavigationController以覆盖这样的方向方法。

Eg, If its inside UINavigationController, then you might need to subclass that UINavigationController to override orientation methods like this.

子类UINavigationController(层次结构的顶层viewcontroller将控制方向。)需要将其设置为self.window.rootViewController。

subclassed UINavigationController (the top viewcontroller of the hierarchy will take control of the orientation.) needs to be set it as self.window.rootViewController.

- (BOOL)shouldAutorotate
 {
     return self.topViewController.shouldAutorotate;
 }
 - (NSUInteger)supportedInterfaceOrientations
 {
     return self.topViewController.supportedInterfaceOrientations;
 }

从iOS 6开始,UINavigationController不会询问其UIVIewControllers定向支持。因此我们需要对其进行子类化。

From iOS 6, it is given that UINavigationController won't ask its UIVIewControllers for orientation support. Hence we would need to subclass it.

MOREOVER

然后,对于UIViewControllers ,你只需要PORTRAIT模式,写下这些函数

Then, For UIViewControllers, in which you need only PORTRAIT mode, write these functions

- (BOOL)shouldAutorotate
{
    return YES;
}

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

对于需要LANDSCAPE的UIViewControllers,将屏蔽更改为All。

For UIViewControllers, which require LANDSCAPE too, change masking to All.

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

现在,如果你想在Orientation改变时做一些改变,那么使用这个函数。

Now, if you want to do some changes when Orientation changes, then use this function.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{

}

非常重要

,写这个。这是非常重要的。

in AppDelegate, write this. THIS IS VERY IMP.

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

如果您只想为所有视图控制器提供纵向模式,请应用肖像面具。即UIInterfaceOrientationMaskPortrait

If you want to provide only Portrait mode for all your viewcontrollers, then apply the portait mask. i.e UIInterfaceOrientationMaskPortrait

否则,如果您希望某些UIViewControllers保留在Portrait中,而其他人支持所有方向,则应用ALL Mask。即UIInterfaceOrientationMaskAll

Otherwise, if you want that some UIViewControllers stay in Portrait while others support all orientations, then apply an ALL Mask. i.e UIInterfaceOrientationMaskAll

这篇关于使UiViewController保持纵向模式iOS6 VS iOS5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆