我的应用程序将在横向启动,只有一个视图可以更改为纵向,其中有一个按钮,我单击此按钮将方向更改为纵向。 它在iPad(iOS 8.4,部署目标:7.0)上按预期执行,我制作了一个iPhone版本,我在UI上做了一些调整,它在iPhone 6 Plus模拟器上运行(Base SDK:9.1,部署目标: 7.0),但是查找方向的按钮不起作用。 我想知道如何在iOS 9中实现这一点; 以下是我的代码:
- (void)changeOrientation
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation==UIInterfaceOrientationLandscapeLeft||orientation==UIInterfaceOrientationLandscapeRight){
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
if (orientation==UIInterfaceOrientationLandscapeLeft) {
value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
}else {
value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown];
}
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ScreenToPortrait" object:nil];
}else{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
if (orientation==UIInterfaceOrientationPortrait) {
value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
}else {
value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
}
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ScreenToLandscapeLeft" object:nil];
}
[UIViewController attemptRotationToDeviceOrientation];
}
答案 0 :(得分:1)
答案 1 :(得分:1)
这适用于iOS9
NSNumber *orientationValue = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue: orientationValue forKey:@"orientation"];
General
标签,然后启用所有方向。supportedInterfaceOrientations()
并返回横向方向值的所有视图。