gpt4 book ai didi

ios - 以编程方式为特定 View Controller 设置设备方向

转载 作者:行者123 更新时间:2023-11-29 00:03:54 25 4
gpt4 key购买 nike

我想以编程方式将设备方向设置为特定 View Controller 的横向模式,如果我与 UIAlertController 一起使用,使用是/否选项,它工作正常,但如果我直接在按钮单击时编写相同的代码。它不起作用。

使用 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];

上面的代码运行良好,但我想在不询问警报的情况下强制下一个 Controller 处于横向模式,所以直接在按钮点击上编写代码,这样,

  NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[self.navigationController pushViewController:mySecondViewController animated:NO];

但是同样的代码运行不正常。它将首先以纵向方式打开 mySecondViewController,然后我们必须手动倾斜手机一次,然后它才会进入横向。

我尝试了很多选择,但没有运气。使用 iOS 10.1 在 iPhone5 和 iPhone6 上测试


更新:添加 mySecondViewController 代码,这是第二个 View Controller 代码,

-(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;
}

最佳答案

尝试此方法强制 View 在 swift 中以横向模式启动:在 viewWillAppear 中使用此代码

let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(Int(value), forKey: "orientation")

override func shouldAutorotate() -> Bool {
return true
}

关于ios - 以编程方式为特定 View Controller 设置设备方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48656569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com