Ios 按语法为特定viewcontroller设置设备方向
我想以编程方式将设备方向设置为特定的viewcontroller的横向模式,如果我与UIAlertController一起使用,使用YES/NO选项,它会工作得很好,但是如果我在单击按钮时直接编写相同的代码。它不起作用 使用UIAlertController的工作代码Ios 按语法为特定viewcontroller设置设备方向,ios,objective-c,ios10,Ios,Objective C,Ios10,我想以编程方式将设备方向设置为特定的viewcontroller的横向模式,如果我与UIAlertController一起使用,使用YES/NO选项,它会工作得很好,但是如果我在单击按钮时直接编写相同的代码。它不起作用 使用UIAlertController的工作代码 UIAlertController * alert=[UIAlertController alertControllerWithTitle:APP_NAME message:@"Please Select Video Orien
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;
}
这是用于设置方向的代码,但如果您想要特定的VC,请在该VC类中尝试此代码
[[UIDevice currentDevice] setValue:
[NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
forKey:@"orientation"];
尝试此操作可强制视图在swift中以横向模式启动: 在ViewWillDisplay中使用此代码
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(Int(value), forKey: "orientation")
及
[UIViewController尝试旋转到设备定向];应在上述行之后调用。
override func shouldAutorotate() -> Bool {
return true
}