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

5 iphone portrait ios landscape-portrait uideviceorientation

我不希望我的应用程序被美化,并始终是porttrait.So我使我的应用程序部署信息只设置肖像.但是当我需要在我的应用程序中显示任何图像或视频时,我需要横向模式以便更好地显示.

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

[[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;
};
}
Run Code Online (Sandbox Code Playgroud)

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

不起作用

NSNumber *value=[NSNumber numberWithInt: UIDeviceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
Run Code Online (Sandbox Code Playgroud)

San*_*u C 7

请从项目设置中启用纵向和横向.然后使用下面的方法让所有viewcontroller自动关闭 -

- (BOOL)shouldAutorotate {
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

然后使用以下方法来除LandScapeViewControler以外的所有方法 -

- (void)viewWillAppear:(BOOL)animated {

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

使用以下方法为LandScapeViewControler -

- (void)viewWillAppear:(BOOL)animated {

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

如果您正在使用NavigationController和TabBarController,请使用类别关闭自动旋转.

希望这会对你有所帮助.

  • 不幸的是,我的**应该没有被调用.你知道为什么吗? (2认同)

Mik*_*ika 6

两个步骤:

  1. 将环境包含在部署中
  2. 在每个视图控制器中,viewDidLoad您需要包括:

    //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"];
    
    Run Code Online (Sandbox Code Playgroud)

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