Ios 当应用程序部署信息肖像被锁定时,如何手动设置设备方向?
我不希望我的应用程序被美化,总是porttrait。所以我让我的应用程序部署信息只设置肖像。但当我需要在我的应用程序中显示任何图像或视频时,我需要横向模式来更好地显示 我可以通过以下方式检测设备方向的变化:Ios 当应用程序部署信息肖像被锁定时,如何手动设置设备方向?,ios,iphone,landscape-portrait,portrait,uideviceorientation,Ios,Iphone,Landscape Portrait,Portrait,Uideviceorientation,我不希望我的应用程序被美化,总是porttrait。所以我让我的应用程序部署信息只设置肖像。但当我需要在我的应用程序中显示任何图像或视频时,我需要横向模式来更好地显示 我可以通过以下方式检测设备方向的变化: [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selecto
[[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"];
两个步骤:
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"];
另请参见:您不应强制改变方向,而应将导航控制器分为横向导航控制器的子类,并通过显示来使用它。然后,你的应用程序将始终是你想要的肖像。当您使用此导航控制器时,它将横向移动
#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];
请从项目设置中启用纵向和横向。然后对所有viewcontroller使用以下方法关闭自动旋转-
- (BOOL)shouldAutorotate {
return NO;
}
然后使用以下方法对除LandscapeViewController之外的所有对象执行操作-
- (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,请使用类别关闭自动旋转
希望这将对您有所帮助。更新了Swift 3+的答案:
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
除了照片和视频,我的应用程序不需要风景。将“横向部署”包括在内没有任何意义。如果您有任何这样做的场景,您无法完全阻止它。视频和图像可能显示在视图控制器中,因此仅将该视图控制器设置为使用QLPreviewController查看文件的Landscapeim,您就偏离了我的问题“如何在部署信息中启用纵向锁定的情况下设置方向?如果在部署信息中设置方向,则目标将不支持未选中的选项。所以我要告诉你的是,你不能用你想要的方式来做。明白你的意思了。我正在尝试1.检查部署信息2中的纵向和横向。-(BOOL)shouldAutorotate{返回否;}在除横向视图外的所有视图中,强制在视图中设置纵向默认值加载除横向视图外的所有视图。但是这也没用:(@Mikaelwhy确实需要这个方法LandScapeViewControler--(void)viewwillbeen:(BOOL)动画?我不能在LandscapeViewController中将shouldAutorotate设置为“是”,它可以旋转到LandScapeleft、right、纵向、upsidedown。如果您想将LandScapeViewControler仅限制为横向模式。否则您可以返回“是”。幸运的是,我的shouldAutorotate没有被调用。您知道为什么吗?请检查此链接,谢谢.it。它大部分工作正常,但在测试过程中出现了几(2)次崩溃。我想这不是什么大问题;但这显然是一个“未记录”的功能,应该如此对待。我将寻找另一种方法。