gpt4 book ai didi

iphone - 技巧 preferredInterfaceOrientationForPresentation 在 vi​​ewController 更改时触发

转载 作者:可可西里 更新时间:2023-11-01 03:27:27 25 4
gpt4 key购买 nike

我正在使用 MSNavigationPaneViewController from here并进行轮换工作。我已经覆盖了根 UINavigationController

中的旋转方法
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.topViewController != nil) {
return [appDelegate.topViewController supportedInterfaceOrientations];
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.topViewController != nil) {
return [appDelegate.topViewController preferredInterfaceOrientationForPresentation];
} else {
return UIInterfaceOrientationPortrait;
}
}

我使用 presentViewController: animated:completion: 推送 MSNavigationPaneViewController 并且我有旋转功能,所以某些 View 可以有不同的方向。问题是,每个具有不同方向的 View 都需要用户倾斜手机以更改方向,此时它会锁定在正确的方向上。我已经阅读了大量的资料来使它正常工作,似乎我需要 preferredInterfaceOrientationForPresentation 在加载每个 View 之前触发,但它没有触发。我认为它没有触发是因为 MSNavigationPaneViewController 没有使用 presentViewController 更改 View Controller 。这是 MSNavigationPaneViewController 用来改变 View 的代码

- (void)setPaneViewController:(UIViewController *)paneViewController
{
if (_paneViewController == nil) {

paneViewController.view.frame = _paneView.bounds;
_paneViewController = paneViewController;
[self addChildViewController:_paneViewController];
[_paneView addSubview:_paneViewController.view];
[_paneViewController didMoveToParentViewController:self];

} else if (_paneViewController != paneViewController) {

paneViewController.view.frame = _paneView.bounds;
[_paneViewController willMoveToParentViewController:nil];
[self addChildViewController:paneViewController];

void(^transitionCompletion)(BOOL finished) = ^(BOOL finished) {
[_paneViewController removeFromParentViewController];
[paneViewController didMoveToParentViewController:self];
_paneViewController = paneViewController;
};

[self transitionFromViewController:_paneViewController
toViewController:paneViewController
duration:0
options:UIViewAnimationOptionTransitionNone
animations:nil
completion:transitionCompletion];
}
}

这看起来只是切换了 viewController,因此没有调用 preferredInterfaceOrientationForPresentation

有什么方法可以强制调用 preferredInterfaceOrientationForPresentation,或者通过隐藏 View 欺骗它来调用它?

谢谢

编辑----

我完全理解新的轮换系统在 iOS6 中是如何工作的,并且要深入到根本来管理它。然而,preferredInterfaceOrientationForPresentation 似乎只有在使用正确方法之一呈现 下一个 View 时才会被调用。如果只是切换窗口则不会。所以我确实需要一种方法来欺骗它被调用。

最佳答案

您可以通过重置其 rootViewController 来欺骗窗口重新评估其支持的界面方向。

UIApplication *app = [UIApplication sharedApplication];
UIWindow *window = [[app windows] objectAtIndex:0];
UIViewController *root = window.rootViewController;
window.rootViewController = nil;
window.rootViewController = root;

您需要确保当此代码执行时, Root View Controller 的 -supportedInterfaceOrientations 返回正确的掩码(对应于您想要强制方向的方式)。

请注意,此 hack 可能会导致屏幕快速闪烁并导致任何正在进行的动画出现故障。

关于iphone - 技巧 preferredInterfaceOrientationForPresentation 在 vi​​ewController 更改时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13934807/

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