swift – 被忽略的preferredStatusBarUpdateAnimation
发布时间:2020-12-14 05:01:56 所属栏目:百科 来源:网络整理
导读:我有像这样呈现MainViewController的AuthViewController: let mainVC = MainViewContoller()mainVC.modalTransitionStyle = .CrossDissolveauthVC.presentViewController(mainVC,animated: true,completion: nil) 我希望AuthViewController隐藏状态栏,但要
我有像这样呈现MainViewController的AuthViewController:
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()覆盖.状态栏出现时没有动画. 我只能通过将MainViewController上的prefersStatusBarHidden设置为true来实现动画,直到viewDidAppear,然后调用: UIView.animateWithDuration(0.3) { self.setNeedsStatusBarAppearanceUpdate() } 我不想每次都打电话.我究竟做错了什么? 解决方法
一直在寻找解决方案.但是,你似乎已经获得了一半的解决方案.你必须为变量设置覆盖,但对我来说最好的做法是为每个覆盖创建一个私有变量对,并让每个覆盖返回它各自的私有变量(见下文).然后,您将更改私有变量并调用setNeedsStatusBarAppearanceUpdate.
另外,您需要在info.plist中将基于View控制器的状态栏外观设置为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() }) }) } 你最终必须调整每个视图控制器,但它似乎是你想要做的,所以希望它有所帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |