如何在iOS 6中以编程方式更改设备方向

utt*_*tam 45 objective-c uiinterfaceorientation ios

iOS 5中,我们可以通过编程方式更改设备方向,如下所示:

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
Run Code Online (Sandbox Code Playgroud)

但是在iOS 6 setOrientation中已弃用,我如何在iOS 6中以编程方式更改设备方向?

Luk*_*ela 49

这是我的"五美分",在iOS7上使用ARC测试

[[UIDevice currentDevice] setValue:
                      [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                            forKey:@"orientation"];
Run Code Online (Sandbox Code Playgroud)

这不会像performSelector那样产生"泄漏"警告.

UIAlertView - 使用此代码,当您在视图(将/将出现)期间打开UIAlertView时,您会注意到除了此视图之外的所有视图都是纵向的(苹果,真的吗?)我无法强制视图重新定向但是发现如果在打开UIAlertView之前稍微延迟,然后视图有时间改变方向.

注意我将从2014年9月12日开始发布我的应用周,如果通过或失败,我会更新帖子.

  • Apple拒绝这个吗?你知道了吗?@ Lukasz'Severiaan'Grela (3认同)
  • 适用于iOS 8. (2认同)

Bal*_*ala 20

这不会回答如何更改设备方向,而是可以帮助您的其他信息.

iOS 6 UI界面方向 - shouldAutorotateToInterfaceOrientation:不工作

方法shouldAutorotateToInterfaceOrientation:iOS 6不支持.不推荐使用.如果你是一个新手,只是盯着可可工作,并想知道为什么你的视图控制器搞砸了iOS 6并且在iOS 5中完美,只要知道shouldAutorotateToInterfaceOrientation:不再受支持了.尽管它可能适用于Xcode 4到4.3,但它不适用于Xcode 4.5.

Apple以更清洁的方式提供了一种新方法来完成这项工作.您可以使用supportedInterfaceOrientations.它返回视图控制器支持的所有接口方向,即接口方向值的掩码.

UIInterfaceOrientationMask枚举:

这些常量是掩码位,用于指定视图控制器支持的接口方向.

typedef enum {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape =
        (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll =
        (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
    UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown =
        (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
    UIInterfaceOrientationMaskLandscapeRight),
} UIInterfaceOrientationMask;
Run Code Online (Sandbox Code Playgroud)

使用shouldAutorotateToInterfaceOrientation:方法:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationIsLandscapeRight(toInterfaceOrientation);
}
Run Code Online (Sandbox Code Playgroud)

使用supportedInterfaceOrientations方法:

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscapeRight;
}
Run Code Online (Sandbox Code Playgroud)

这些是UIViewController添加的关于iOS6中的Orientation的方法

  1. UIViewController preferredInterfaceOrientationForPresentation

  2. UIViewController应该是Autorotate

  3. UIViewController supportedInterfaceOrientations

在iOS6中添加了关于Orientation的UIApplication方法

  1. UIApplication supportedInterfaceOrientationsForWindow:

  2. UIInterfaceOrientationMask

  • -1这不回答如何以编程方式更改设备方向的问题. (4认同)
  • 谢谢你的ans,但我想以编程方式更改方向.如何使用[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]改变我们在ios5中所做的方向; (3认同)
  • 这不是问题的答案.这更像是附加信息. (2认同)

P.L*_*.L. 20

我发现强制设备改变方向的最简单方法是提供一个新的视图控制器(使用presentViewController:animated:completion:),其中新的视图控制器指定了一个特定的首选方向(通过实现该方法-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation).

当按预期显示新的视图控制器时,方向将更改为新视图控制器首选的方向.那么,最简​​单的实现(最佳实践?)是将您在特定方向所需的所有功能嵌入到单独的视图控制器中,并根据需要显示它.系统将负责为您更改方向.

显然,这可能不适合所有用例,但幸运的是,同样的技巧适用于强制设备更改现有视图控制器的方向.

诀窍是使用您需要的特定首选方向呈现一个新的视图控制器,然后立即隐藏它.这将导致在显示新视图控制器时临时更改方向.最好的部分是,当新视图控制器被解除时,preferredInterfaceOrientationForPresentation再次查询原始(呈现)视图控制器 ,您可以在此处指定所需的最终方向.

这里要注意的一件重要事情是在原始视图控制器中临时禁用自动旋转(当从新呈现的当时被解除的视图控制器返回时),这样当用户将手机旋转到新的方向时,它不会触发进一步的自动旋转

下面的代码应该说明我的观点,我的例子强制旋转到肖像,如果你想要其他方向,只需相应地改变.

假设您具有名为的原始视图控制器Original和名为的临时视图控制器ForcePortrait

@interface Original : UIViewController
{
    BOOL orientationToPortrait; //should set to NO by default
}
@end

@implementation Original
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation
{
    if(orientationToPortrait)
    {
        //when we manually changed, show in Portrait
        return UIInterfaceOrientationPortrait;
    }
    else
    {
        //before manual orientation change, we allow any orientation
        return self.interfaceOrientation;
    }
}

-(BOOL) shouldAutorotate
{
    //we should 'lock' the rotation once we manually change it
    return !orientationToPortrait;
}

-(void) changeOrientationToPortrait
{
    //Sample method to change the orientation
    //when called, will show (and hide) the temporary view
    //Original.preferredInterfaceOrientationForPresentation will be called again after this method

    //flag this to ensure that we tell system we prefer Portrait, whenever it asked again
    orientationToPortrait = YES;

    //presenting the following VC will cause the orientation to temporary change
    //when the new VC is dismissed, system will ask what is our (Original) orientation preference again
    ForcePortrait* forcePortrait = [[ForcePortrait alloc] init];
    [self presentViewController:forcePortrait animated:NO completion:^{
        [forcePortrait dismissViewControllerAnimated:NO completion:nil];
    }];
}


@end

@interface ForcePortrait : UIViewController
@end

@implementation ForcePortrait
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
@end
Run Code Online (Sandbox Code Playgroud)


小智 10

试试这个:

#import <objc/message.h>

if(UIDeviceOrientationIsLandscape(self.interfaceOrientation)){
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
        {
            objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait );
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 此选项为+1.可能会被Apple拒绝,但这是我对企业应用所需要的. (2认同)

小智 5

您应该放在
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
AppDelegate didFinishLaunchingWithOptions方法中.

然后,在您的应用程序的任何位置,您都可以获得当前的方向:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
Run Code Online (Sandbox Code Playgroud)

测试方向:

UIInterfaceOrientationIsPortrait(orientation) 
UIInterfaceOrientationIsLandscape(orientation)
Run Code Online (Sandbox Code Playgroud)

就像,喜欢

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
    // code for landscape orientation
     // OR
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
     //  OR
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft];

}
else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
    // code for Portrait orientation
    //  OR
   [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortraitUpsideDown];
    //  OR
   [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}
Run Code Online (Sandbox Code Playgroud)

  • `例如,消息类型'UIDevice'消息没有声明带有选择器'setOrientation:'的方法 (3认同)

归档时间:

查看次数:

74967 次

最近记录:

6 年,1 月 前