当前位置: 首页 > 知识库问答 >
问题:

当应用程序部署信息纵向锁定时,如何手动设置设备方向?

上官高逸
2023-03-14

我不希望我的应用程序被美化并始终是porttrait。所以我让我的应用程序部署信息仅设置纵向。但是当我需要在我的应用程序中显示任何图像或视频时,我需要横向模式才能更好地显示。

我可以通过检测设备方向的变化

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationChanged:)
 name:UIDeviceOrientationDidChangeNotification
 object:[UIDevice currentDevice]];

- (void) orientationChanged:(NSNotification *)note
{
UIDevice * device = note.object;
switch(device.orientation)
{
    case UIDeviceOrientationPortrait:
        /*  */
        break;

    case UIDeviceOrientationPortraitUpsideDown:
        /*  */
        break;
    case UIDeviceOrientationLandscapeLeft:
       /*  */
       break;

    case UIDeviceOrientationLandscapeRight:
       /*  */
       break;
    default:
        break;
};
}

但是,即使应用程序部署信息肖像被锁定,我如何手动更改设备方向?

这不起作用

NSNumber *value=[NSNumber numberWithInt: UIDeviceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 

共有3个答案

洪通
2023-03-14

与其强制改变方向,不如将导航控制器分为横向的子类,并通过呈现来使用它。然后你的应用程序将始终是你想要的肖像。当您使用此导航控制器时,它将横向移动。

#import "RotationAwareNavigationController.h"

@implementation RotationAwareNavigationController

-(BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

你应该这样称呼它;

RotationAwareNavigationController *navController = [[RotationAwareNavigationController alloc] initWithRootViewController:aViewController];
[self presentViewController:navController animated:NO completion:nil];
高玮
2023-03-14

两个步骤:

>

  • 将景观包含到部署中
  • 在每个视图控制viewddLoad中,您需要包括:

    //Swift
    let value = UIInterfaceOrientation.LandscapeLeft.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
    
    //Obj-C
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    

    另请参见:如何在iOS7中以编程方式设置设备方向?

  • 呼延庆
    2023-03-14

    请启用肖像

    - (BOOL)shouldAutorotate {
        return NO;
    }
    

    然后对除LandScapeViewControler之外的所有对象使用以下方法-

    - (void)viewWillAppear:(BOOL)animated {
    
        [[UIDevice currentDevice] setValue:
         [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                                    forKey:@"orientation"];
    }
    

    对LandScapeViewControler使用以下方法-

    - (void)viewWillAppear:(BOOL)animated {
    
            [[UIDevice currentDevice] setValue:
             [NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft]
                                        forKey:@"orientation"];
        }
    

    如果您使用的是NavigationController和TabBarController,请使用category关闭自动旋转。

    希望这对你有帮助。

     类似资料:
    • 我想知道如何旋转设备,即使用户在iPhone打开肖像方向锁定。就像亚马逊黄金视频应用程序一样,通常我看到应用程序是竖屏的,但是当我开始看电影时,即使我打开肖像方向锁定,应用程序也会将其旋转变为水平。 当我在谷歌上搜索时,我只找到了这篇文章,但使用CoreMotion是实现我想要做的事情的唯一方法?? 我使用以下代码从本文中旋转设备, 但该应用程序不会自动从纵向旋转到横向(从横向到纵向工作...),

    • 我正在开发一个Android相机api应用程序,它被锁定在清单文件的竖屏中。 我的问题是,当我存储图片时,它们都以相同的方向存储。 我希望它们以真实的方向存储,例如,我的天花板应该位于存储图片的顶部,无论我如何定位手机,如果这有意义的话 为了做到这一点,我必须知道设备的方向,然而,当应用程序锁定在纵向时,所有getOrientation的方法都认为我的手机处于纵向模式,无论我手机的物理方向如何。

    • setPrintedPortrait(): self 实例 $config = ['path' => './tests']; $excel = new \Vtiful\Kernel\Excel($config); ​ $excel->fileName('printed_portrait.xlsx', 'sheet1') ->setPrintedPortrait() // 设置打印方向为

    • 我创建了一个android应用程序,它必须设置设备的时间,因为设备不能记住时间。不知道如何设置设备的时间。

    • 我正在使用AutoLayout开发iPad应用程序,如果用户启用某个模式(“平视”模式),我希望只支持纵向(或纵向倒置)方向,而且,如果设备处于横向,我希望自动切换到纵向模式。 在顶视图控制器中,我有以下内容: 根据我在这里其他地方看到的答案,答案似乎是我应该使用“application SetStatusBaroOrientation”。因此,在用户选择“抬头”模式的方法中,我包括: 然而,这似

    • 我有一个情况,我们有一个使用网络视图的Android应用程序。当用户导航到YouTube视频时,它开始播放(带有音频),然后用户使用设备的硬件开关锁定设备,它会继续播放音频。 当应用程序使用设备菜单发送到后台或退出应用程序时,不会发生这种情况。 有人知道为什么会发生这种情况以及如何阻止它吗? 编辑:我刚刚在这里发现了一个类似的、未回答的帖子:应用程序支持视频的后台播放、科尔多瓦问题、谷歌播放拒绝应