【发布时间】:2018-02-07 05:26:25
【问题描述】:
我想以编程方式将设备方向设置为横向模式到特定的视图控制器,如果我与 UIAlertController 一起使用,它工作得很好,带有 YES/NO 选项,但如果我直接在按钮单击时编写相同的代码。它不工作。
使用 UIAlertController 工作的代码,
UIAlertController * alert=[UIAlertController
alertControllerWithTitle:APP_NAME message:@"Please Select Video Orientation."preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Landscape Mode"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[self.navigationController pushViewController:mySecondViewController animated:NO];
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:@"Portrait Mode"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[self.navigationController pushViewController:mySecondViewController animated:NO];
}];
[alert addAction:yesButton];
[alert addAction:noButton];
[self presentViewController:alert animated:YES completion:nil];
上面的代码运行良好,但我想强制下一个控制器处于横向模式而不询问警报,所以直接在按钮单击时编写代码,如下所示,
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[self.navigationController pushViewController:mySecondViewController animated:NO];
但是同样的代码不能正常工作。它将首先以纵向打开 mySecondViewController,然后我们必须手动倾斜手机一次,然后它将进入横向。
我尝试了很多选择,但没有运气。使用 iOS 10.1 在 iPhone5 和 iPhone6 上测试
更新:添加 mySecondViewController 代码, 这是第二个视图控制器代码,
-(void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidAppear:(BOOL)animated {
// [super viewDidAppear:animated];
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft|UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
-(BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
【问题讨论】:
标签: ios objective-c ios10