[英]How to change device orientation when app returns to foreground from background in iOS?
[英]How to change device orientation when app returns to foreground in iOS?
我已使用以下代码在子视图控制器中将方向从纵向更改为横向
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
但是当我进入背景并返回到前景时,我按下按钮转到主页,然后需要使用以下代码将方向从横向更改为纵向
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([ConfigObjC currentPage] && [[ConfigObjC currentPage] isEqualToString:@"SubViewController"])
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
else{
return UIInterfaceOrientationMaskPortrait;
}
}
但它不起作用。
当我不去背景的时候,我已经成功地将风景从肖像改为肖像。 我该如何解决此问题,非常感谢
如果要将窗口置于横向模式,则可以将Xcode信息修改为仅支持横向,如下所示:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
编码的另一种方法是更改UIWindow
方向,如下所示:
UIApplication *application=[UIApplication sharedApplication];
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
application.keyWindow.transform=CGAffineTransformMakeRotation(M_PI);
上面使用这种方式需要将ShouldAutoRotate
设置为NO
。
- (BOOL)shouldAutorotate{
return NO;
}
此外,如果在横向模式下仅显示几个视图,则可以使用属性transform
来旋转这些视图,如下所示:
//rotate screen only with view
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
希望可以帮到您!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.