gpt4 book ai didi

ios - UIViewController 的 prefersStatusBarHidden 不起作用

转载 作者:IT老高 更新时间:2023-10-28 11:41:13 24 4
gpt4 key购买 nike

我正在尝试隐藏我的一个 View Controller 的状态栏(当模态显示时)。当我展示 View Controller 时,状态栏将被隐藏,然后在关闭时返回。

我已将以下代码添加到呈现的 View Controller 中

- (BOOL)prefersStatusBarHidden
{
return YES;
}

我还将 Info.plist 文件中的键设置为以下内容:

<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>

据我了解,这应该是完成这项工作所需的全部内容。

我还使用自定义动画 Controller 进行呈现,它符合 UIViewControllerAnimatedTransitioning 协议(protocol)。在 animateTransition: 实现中,我尝试手动调用 prefersStatusBarHidden,然后是 setNeedsStatusBarAppearanceUpdate 以确保调用正在进行,但状态栏仍然存在.

任何想法为什么会发生这种情况将不胜感激。我已经搜索过 StackOverflow,但似乎没有人遇到过这个问题,所有接受的答案都是指调用 setNeedsStatusBarAppearanceUpdate,我已经在这样做了。

EDIT - 下面的代码现在似乎 WORK 符合需要

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
if (self.isPresenting) {
UIView *containerView = [transitionContext containerView];

UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

toViewController.view.frame = containerView.frame;

[containerView addSubview:toViewController.view];

// Ask the presented controller whether to display the status bar
[toViewController setNeedsStatusBarAppearanceUpdate];

[UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
toViewController.view.alpha = 1.0f;
fromViewController.view.alpha = 0.0f;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
else {
// do the reverse
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

[UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
toViewController.view.alpha = 1.0f;
fromViewController.view.alpha = 0.0f;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
// Once dismissed - ask the presenting controller if the status bar should be presented
[toViewController setNeedsStatusBarAppearanceUpdate];
}];
}
}

....

// PresentingController.m
- (BOOL)prefersStatusBarHidden
{
if (self.presentedViewController) {
return YES;
}
return NO;
}

// PresentedController.m
- (BOOL)prefersStatusBarHidden
{
return YES;
}

最佳答案

在 iOS7 中,UIViewController 实际上有一个新属性,称为 modalPresentationCapturesStatusBarAppearanceApple iOS reference.

Default value is NO.

When you present a view controller by calling the presentViewController:animated:completion: method, status bar appearance control is transferred from the presenting to the presented view controller only if the presented controller’s modalPresentationStyle value is UIModalPresentationFullScreen. By setting this property to YES, you specify the presented view controller controls status bar appearance, even though presented non–fullscreen.

The system ignores this property’s value for a view controller presented fullscreen.

因此,对于普通全屏以外的任何presentationStyle(例如:UIModalPresentationCustom),如果你想捕获状态栏,这个必须设置。要使用,您只需在正在呈现的 View Controller 上将其设置为 YES:

toVC.modalPresentationCapturesStatusBarAppearance = YES;

关于ios - UIViewController 的 prefersStatusBarHidden 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23615647/

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