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;
};
}
但是,即使应用程序部署信息纵向被锁定,如何手动更改设备方向?
这不起作用
NSNumber *value=[NSNumber numberWithInt: UIDeviceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
请从项目设置中启用纵向和横向.然后使用下面的方法让所有viewcontroller自动关闭 -
- (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,请使用类别关闭自动旋转.
希望这会对你有所帮助.
两个步骤:
在每个视图控制器中,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"];
另请参阅:如何在iOS7中以编程方式设置设备方向?
| 归档时间: | 
 | 
| 查看次数: | 9832 次 | 
| 最近记录: |