无法在iOS 6中自动旋转

问题描述 投票:0回答:4

[在我正在维护的应用中,应该在纵向和纵向倒置模式下发生旋转。 (所有轮播均在摘要面板中启用。)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown;
}

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

无论我尝试了什么,我都无法使旋转精确到i ios 6

到目前为止我尝试过的事情:

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait; 
}

-(NSInteger)supportedInterfaceOrientations{
NSInteger mask = 0;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight])
    mask |= UIInterfaceOrientationMaskLandscapeRight;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeLeft])
    mask |= UIInterfaceOrientationMaskLandscapeLeft;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortrait])
    mask |= UIInterfaceOrientationMaskPortrait;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortraitUpsideDown])
    mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
return mask;
}

我尝试将其放入我的appdelegate:

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

但是我收到此错误:由于未捕获的异常'UIApplicationInvalidInterfaceOrientation',终止应用程序,原因:'支持的方向与应用程序没有共同的方向,并且shouldAutorotate返回YES'

试图将此放到我的代表中:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations = UIInterfaceOrientationMaskAll;

    if (self.window.rootViewController) {
        UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presented supportedInterfaceOrientations];
    }
    return orientations; 
}

我阅读了有关它的所有讨论以及对shouldAutorotateToInterfaceOrientation的弃用,但我仍然无法使其正常工作。

我要丢失它

objective-c cocoa-touch cocoa ios6
4个回答
2
投票

摘自Apple的iOS 6 SDK发行说明:

iOS 6中的自动旋转正在更改。在iOS 6中,不赞成使用shouldAutorotateToInterfaceOrientation:UIViewController方法。应当使用supportedInterfaceOrientationsForWindow:shouldAutorotate方法代替它。

[更多责任转移到应用程序和应用程序委托。现在,iOS容器(例如UINavigationController)不再咨询其子级来确定是否应自动旋转。默认情况下,应用程序和视图控制器的受支持的界面方向对于iPad习惯设置为UIInterfaceOrientationMaskAll,对于iPhone习惯设置为UIInterfaceOrientationMaskAllButUpsideDown

View Controller的受支持的界面方向可能会随时间变化-即使应用程序的受支持的界面方向也会随时间变化。每当设备旋转或以全屏模式呈现方式呈现视图控制器时,系统都会向最上方的全屏视图控制器(通常是根视图控制器)询问其支持的界面方向。此外,仅当此视图控制器从其YES方法返回shouldAutorotate时,才检索受支持的方向。系统将视图控制器的支持方向与应用程序支持的方向(由Info.plist文件或应用程序代表的application:supportedInterfaceOrientationsForWindow:方法确定)相交,以确定是否旋转。

[系统通过将应用程序的supportedInterfaceOrientationsForWindow:方法返回的值与最顶部的全屏控制器的supportedInterfaceOrientations方法返回的值相交来确定是否支持方位。 setStatusBarOrientation:animated:方法未完全弃用。现在,仅当最顶部的全屏视图控制器的supportedInterfaceOrientations方法返回0时,此方法才有效。这使调用方负责确保状态栏方向一致。

出于兼容性考虑,仍然实现shouldAutorotateToInterfaceOrientation:方法的视图控制器不会获得新的自动旋转行为。 (换句话说,它们并不依赖于使用应用程序,应用程序委托或Info.plist文件来确定受支持的方向。)相反,shouldAutorotateToInterfaceOrientation:方法用于合成由[ C0]方法。

如果要旋转整个应用程序,则应将Info.plist设置为支持所有方向。现在,如果只想让特定视图成为纵向视图,则必须执行某种子类,并重写自动旋转方法以仅返回纵向视图。只需看看supportedInterfaceOrientations


1
投票

我的应用程序以IOS 5为目标。我对IOS 5设备使用了shouldAutorotateToInterfaceOrientation方法(默认情况下)。并分类UINavigationController来处理IOS 6的方向。

How to force a UIViewController to Portrait orientation in iOS 6

0
投票

检查您的目标属性...看起来像下面

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9kem1ZUi5wbmcifQ==” alt =“在此处输入图像描述”>


0
投票
#import "UINavigationController+Rotation_IOS6.h"

    @implementation UINavigationController (Rotation_IOS6)

    -(BOOL)shouldAutorotate
    {

            return YES;
    }

    -(NSUInteger)supportedInterfaceOrientations
    {

        return UIInterfaceOrientationMaskAll;
    }

    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        if([self.visibleViewController isMemberOfClass:NSClassFromString(@"SampleViewController")])
        {
            return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskPortrait;
        }
        return UIInterfaceOrientationPortrait;

    }

    @end
© www.soinside.com 2019 - 2024. All rights reserved.