- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有 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()
覆盖被忽略。状态栏出现时没有动画。
我只能通过将 MainViewController
上的 prefersStatusBarHidden
设置为 true
直到 viewDidAppear
,然后调用这个:
UIView.animateWithDuration(0.3) {
self.setNeedsStatusBarAppearanceUpdate()
}
我不想每次都调用它。我做错了什么?
最佳答案
也在寻找解决方案。但是,您似乎已经获得了一半的解决方案。您必须为变量设置覆盖,但对我来说最好的做法是为每个覆盖创建一个私有(private)变量对,并让每个覆盖返回它各自的私有(private)变量(见下文)。然后,您将更改私有(private)变量并调用 setNeedsStatusBarAppearanceUpdate
。
另外,您需要将 info.plist
中的 View controller-based status bar appearance
设置为 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()
})
})
}
你最终不得不调整每个 View Controller ,但这似乎是你想要做的,所以希望它能有所帮助!
关于swift - preferredStatusBarUpdateAnimation 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37494266/
我有 AuthViewController 像这样显示 MainViewController: let mainVC = MainViewContoller() mainVC.modalTransit
我是一名优秀的程序员,十分优秀!