8

我有资格问这个问题,因为经过近 2 天的谷歌搜索、堆栈溢出等的大型研究......

我的问题是这样的:我正在从我的主 ViewController 中呈现 ViewController,如下所示:

UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:VController];
navigation.transitioningDelegate = self;
navigation.modalPresentationStyle = UIModalPresentationCustom;

[self presentViewController:navigation
                   animated:YES
                 completion:nil];

每当 iPhone 用户正在通话或将他或她的手机用作热点时,状态栏会放大,将我的模式呈现的 VC 推到底部,但原点设置为 (0;0) 问题是当用户在通话期间完成通话时他在我的应用程序状态栏中调整为正常大小,但 Modal VC 没有向上移动。

在此处输入图像描述

由于此通知,我在代码中发生时就知道了这一点:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statuBarChange:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];

最糟糕的是帧是正确的并且原点仍然是(0,0)

有没有办法引用模态呈现的 vc ?没有解雇并再次提出?

4

2 回答 2

0
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:VController];
navigation.transitioningDelegate = self;
navigation.modalPresentationStyle = UIModalPresentationCustom;

UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

[topController presentViewController:navigation
                   animated:YES
                 completion:nil];
于 2022-02-23T07:12:24.393 回答
0

Do you really need a custom transition for this modal? If not, remove the line "navigation.modalPresentationStyle = UIModalPresentationCustom" and you're good to go.

If you do need a custom style, this is a known bug with the UIModalPresentationCustom style in iOS 8+ and the status bar. AFAIK, you'll have to hack around this with animateTransition and shoving the frames around into the proper places.

There's also an awful hack for this using willChangeStatusBarFrame in the app delegate below. You can improve it with detecting if there's actually a modal up, and animating the change.

- (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame
{
    if (newStatusBarFrame.size.height < 40) {
        for (UIView *view in self.window.subviews) {
            view.frame = self.window.bounds;
        }
    }
}

Another alternative is to make the modal cover the status bar, and override prefersStatusBarHidden for that view controller.

I hate all these solutions, but it should give you something workable depending how your project is set up, and if you can't ignore that little space for a temporary modal dialog.

于 2017-02-07T20:25:03.627 回答