自iOS 9以来如何为状态栏样式更改设置动画 [英] how to animate status bar style change since iOS 9

查看:100
本文介绍了自iOS 9以来如何为状态栏样式更改设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我以前使用:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent 
                                            animated:YES];

为状态栏样式更改设置动画,从暗到亮,反之亦然

to animate the status bar style change, from dark to light and vice versa

但是从iOS 9开始不推荐使用此方法。

However this method is deprecated since iOS 9.

我确实改为使用 preferredStatusBarStyle 关注@ serenn在不会调用preferredStatusBarStyle 的回答

I do changed to use preferredStatusBarStyle following @serenn's answer in preferredStatusBarStyle isn't called

它确实可以改变旧式的状态栏样式,但没有动画。

It indeed can change the status bar style in old fashion, but without animations.

文档说:

如果此方法的返回值发生更改,请调用setNeedsStatusBarAppearanceUpdate方法。

If the return value from this method changes, call the setNeedsStatusBarAppearanceUpdate method.

但是我我不知道在哪里调用它,我试过将它放在viewWillAppear中,但没有运气。

However I have no idea where to call it, I tried put it in viewWillAppear, but no luck.

preferredStatusBarUpdateAnimation 仍为默认值: UIStatusBarAnimationFade

所以我很困惑。寻找如何动画为已弃用方法的答案。提前致谢!

So I am confused. Looking for answers for how to animate as the deprecated method. Thanks in advance!

推荐答案

好的我花了两个小时搜索并尝试,想通了:

OK I have spent two hours searching and trying, figured it out:

首先,您必须确保您的子视图控制器可以通过覆盖 preferredStatusBarStyle 来控制导航控制器中的状态栏样式,例如@ serenn的答案不会调用preferredStatusBarStyle

First, you have to make sure your child view controller can control the status bar style in the navigation controller by overriding preferredStatusBarStyle like @serenn's answer in preferredStatusBarStyle isn't called

按顺序要有动画,我必须在视图控制器显示如下之前和之后返回两种不同的样式:

In order to have animation, I have to return two different style before and after the view controller appears like below:

-(UIStatusBarStyle)preferredStatusBarStyle {
    if (!viewAppeared)
        return UIStatusBarStyleDefault;
    else
        return UIStatusBarStyleLightContent; // your own style
}

viewAppeared 是一个BOOL,用于指示是否调用 viewWillAppear

viewAppeared is a BOOL to indicate if the viewWillAppear is called:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    viewAppeared = YES;

    [UIView animateWithDuration:0.8 animations:^{
        [self setNeedsStatusBarAppearanceUpdate];
    }];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    viewAppeared = NO;
}

因此,当视图控制器没有出现时,状态栏样式为 UIStatusBarStyleDefault 并将在 viewWillAppear 中更改为 UIStatusBarStyleLightContent

So when the view controller does not appear, the status bar style is UIStatusBarStyleDefault and will be changed to UIStatusBarStyleLightContent in viewWillAppear.

然后调用 setNeedsStatusBarAppearanceUpdate 更新如下所示的更改,注意 animateWithDuration 是必须有动画。

Then call setNeedsStatusBarAppearanceUpdate to update the change like below, be noted animateWithDuration is a must to have animation.

[UIView animateWithDuration:0.8 animations:^{
    [self setNeedsStatusBarAppearanceUpdate];
}];

我曾经发现你不必打电话给 preferredStatusBarStyle 首先如下所示, setNeedsStatusBarAppearanceUpdate 将再次调用 preferredStatusBarStyle

I used to find out you don't have to call preferredStatusBarStyle first like below, setNeedsStatusBarAppearanceUpdate will invoke preferredStatusBarStyle again:

[UIView animateWithDuration:0.8 animations:^{
    [self preferredStatusBarStyle];
    [self setNeedsStatusBarAppearanceUpdate];
}];

虽然这个解决方案解决了这个问题,但我现在不选择使用它。我只能使用一行代码获得相同的结果,但现在我需要写的代码太多了。不推荐使用的API现在甚至都没有触发警告,所以在Apple不断推动我改变之前,我不会用这种方式。当时机成熟时,我希望有一个很好的解决方案。

Though this solution solve the problem, I don't choose to use it for now. I can have the same result with only one line code but it is now way too many of the code I have to write. The deprecated API does not even trigger a warning right now, so until Apple keeps pushing me to change, I will not use this way. When the time comes I hope there is a good solution then.

我很困惑为什么Apple弃用了单行代码( [UIApplication sharedApplication] ] setStatusBarStyle] ... )可以做所有的技巧,但使用更复杂的方法来实现相同的结果。即使你想要提供更细粒度的控制,Apple也不必弃用旧版本。

I am very confused why Apple deprecated the one-line code ([UIApplication sharedApplication] setStatusBarStyle]...)that can do all the tricks, but use a more complicated to achieve the same result. Even you want to give more fine-grained control, Apple does not have to deprecated the old one.

但也许我现在没找到最好的控件。希望有人能够让我高兴。

But maybe I just didn't find the best one right now. Hope someone could enlight me.

这篇关于自iOS 9以来如何为状态栏样式更改设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆