PreferredStatusBarUpdateAnimation 被忽略

2024-03-20

I have AuthViewController那就是呈现MainViewController像这样:

let mainVC = MainViewContoller()
mainVC.modalTransitionStyle = .CrossDissolve
authVC.presentViewController(mainVC, animated: true, completion: nil)

我想要AuthViewController隐藏状态栏,但是MainViewController来展示,像这样:

AuthViewController {

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .LightContent
    }

    override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
        return .Fade
    }

    override func prefersStatusBarHidden() -> Bool {
        return false
    }
}

MainViewController {

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .LightContent
    }

    override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
        return .Fade
    }

    override func prefersStatusBarHidden() -> Bool {
        return false
    }
}

状态栏出现,但是preferredStatusBarUpdateAnimation()覆盖被忽略。状态栏不显示动画。

我只能通过设置让它动画化prefersStatusBarHidden on MainViewController to true直到viewDidAppear,然后这样调用:

UIView.animateWithDuration(0.3) {
    self.setNeedsStatusBarAppearanceUpdate()
}

我不想每次都调用这个。我究竟做错了什么?


也一直在寻找解决方案。然而,你似乎已经得到了一半的解决方案。您必须为变量设置覆盖,但对我来说最佳实践是为每个覆盖创建一个私有变量对,并让每个覆盖返回其各自的私有变量(见下文)。然后您可以更改私有变量并调用setNeedsStatusBarAppearanceUpdate.

另外,你需要设置View controller-based status bar appearance在你的info.plist to YES。否则,它仍然会依赖遗产UIApplication.shared...方法。

这是一个片段:

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
    get {
        return .slide
    }
}

private var statusBarStyle : UIStatusBarStyle = .default

override var preferredStatusBarStyle: UIStatusBarStyle {
    get {
        return statusBarStyle
    }
}

private var statusBarStatus : Bool = false

override var prefersStatusBarHidden: Bool {
    get {
        return statusBarStatus
    }
}

然后我可以像这样调用一个函数:(这是我的示例之一,因此请忽略自定义函数)。

func sliderView(sliderView: SliderView, didSlideToPlace: CGFloat, within: CGFloat) {

    let val = (within - (didSlideToPlace - sliderView.upCent))/(within)
    print(val)
    //Where you would change the private variable for the color, for example.
    if val > 0.5 {
        statusBarStyle = .lightContent
    } else {
        statusBarStyle = .default
    }
    UIView.animate(withDuration: 0.5, animations: {
        sliderView.top.backgroundColor = UIColor.black.withAlphaComponent(val)
        self.coverLayer.alpha = val
        self.scroll.backgroundColor = colors.lightBlueMainColor.withAlphaComponent(val)
    }, completion: {
        value in
        //If you do not call setNeedsStatusBarAppearanceUpdate() in an animation block, the animation variable won't be called it seems.
        UIView.animate(withDuration: 0.4, animations: {

            self.animating = true

            //Where you set the status for the bar (your part of the solution)
            self.statusBarStatus = false

            //Then you call for the refresh
            self.setNeedsStatusBarAppearanceUpdate()
        })
    })
}

您最终必须调整每个视图控制器,但这似乎就是您想要做的,所以希望它有帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PreferredStatusBarUpdateAnimation 被忽略 的相关文章

随机推荐