需要创建自定义segue,实现功能是不管手机如何旋转,下一个视图都锁定为竖屏模式。我已经创建了UIStoryBoardSegue 文件,但是我现在横屏到竖屏的转换有问题:
-(void) perform
{
self.appDelegate = [[UIApplication sharedApplication] delegate];
UIViewController *source = (UIViewController *) self.sourceViewController;
UIViewController *destination = (UIViewController *) self.destinationViewController;
}
为了锁定竖屏模式,你可以在 perform 方法中设置 destination 视图控制器的屏幕方向为竖屏,并在转场动画完成后将其还原。
你可以使用以下代码来实现这一点:
- (void) perform
{
UIViewController *destination = (UIViewController *) self.destinationViewController;
UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
if (currentOrientation == UIInterfaceOrientationLandscapeLeft || currentOrientation == UIInterfaceOrientationLandscapeRight) {
[destination setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
}
[self.sourceViewController presentViewController:destination animated:YES completion:nil];
}
需要注意的是,上面的代码只能在 iOS 13 及以下版本上使用。在 iOS 13 及以上版本中,您需要使用新的屏幕方向API来设置目标控制器的屏幕方向。
另外,您可能需要在目标视图控制器中重写 shouldAutorotate 方法并返回 NO,以确保它不会自动旋转。