gpt4 book ai didi

iOS 7+ 关闭模态视图 Controller 并强制纵向

转载 作者:技术小花猫 更新时间:2023-10-29 10:26:42 24 4
gpt4 key购买 nike

我有一个 UINavigationController 作为我在 iOS 7 和 iOS 8 上的 UIWindow 的 Root View Controller 。从它的一个 View Controller ,我展示了一个具有交叉溶解呈现样式的全屏模态视图 Controller 。这个模态视图 Controller 应该能够旋转到所有方向,并且工作正常。

问题是当设备以横向放置并且模态视图 Controller 被关闭时。呈现模态的 View Controller 仅支持纵向,并且我已确认 UIInterfaceOrientationMaskPortrait 已返回到 -application:supportedInterfaceOrientationsForWindow:。 -shouldAutorotate 也返回 YES。但是,呈现 View Controller 的方向在关闭模式后仍保持横向。如何在允许模式采用设备方向的同时强制它保持纵向?我的代码如下:

应用委托(delegate):

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
UINavigationController *navigationController = (UINavigationController *)self.deckController.centerController;
NSArray *viewControllers = [navigationController viewControllers];
UIViewController *top = [viewControllers lastObject];

if (top && [top presentedViewController]) {
UIViewController *presented = [top presentedViewController];
if ([presented respondsToSelector:@selector(isDismissing)] && ![(id)presented isDismissing]) {
top = presented;
}
}

return [top supportedInterfaceOrientations];
}

return (UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight);
}

呈现 View Controller :

- (BOOL)shouldAutorotate {
return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}

模态视图 Controller :

- (BOOL)shouldAutorotate
{
return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskLandscape|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskPortrait);
}

最佳答案

如果模态 Controller 在关闭前处于横向,则呈现的 ViewController 可能不会返回到原始方向(纵向)。问题是因为 AppDelegate supportedInterfaceOrientationsForWindow 方法在 Controller 实际关闭之前被调用,并且呈现的 Controller 检查仍然返回 Landscape mask。

设置一个标志以指示是否显示(模态)presented View Controller 。

- (void)awakeFromNib // or where you instantiate your ViewController from
{
[super awakeFromNib];
self.presented = YES;
}

- (IBAction)exitAction:(id)sender // where you dismiss the modal
{
self.presented = NO;
[self dismissViewControllerAnimated:NO completion:nil];
}

并且在模态呈现的 ViewController 中根据标志设置方向:当模态 ViewController 呈现时 - 返回 Landscape。当它被解雇时,然后返回肖像

- (NSUInteger)supportedInterfaceOrientations
{
if ([self isPresented]) {
return UIInterfaceOrientationMaskLandscape;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}

最后一步 - 从您的 AppDelegate 调用模态呈现的 ViewController 的方向。我只是检查当前呈现的 ViewController 并在其上调用 supportedInterfaceOrientations

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
NSUInteger orientationMask = UIInterfaceOrientationMaskPortrait;

UIViewController *currentVC = self.window.rootViewController.presentedViewController; // gets the presented VC
orientationMask = [currentVC supportedInterfaceOrientations];

return orientationMask;
}

更多信息请查看this link

关于iOS 7+ 关闭模态视图 Controller 并强制纵向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25391564/

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