Navigation Bar is Black during Custom Unwind Segue
I made a custom UIStoryboardSegue
but when it unwinds, it seems to cause my UINavigationBar
to go black, then back to it's correct color. See the GIF below.
My custom segue just makes the the new ViewController come down from the top and leave going back up to the top.
Here is the UIStoryboardSegue
code:
import UIKit
class SlideDownSegue: UIStoryboardSegue {
var duration: Double = 0.5
override func perform() {
let screenHeight = UIScreen.main.bounds.size.height
let toVC = self.destination
let fromVC = self.source
toVC.view.transform = CGAffineTransform(translationX: 0, y: -screenHeight)
UIApplication.shared.keyWindow?.insertSubview(toVC.view, aboveSubview: fromVC.view)
UIView.animate(withDuration: duration, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
toVC.view.transform = CGAffineTransform.identity
}, completion: {
success in
fromVC.present(toVC, animated: false, completion: nil)
})
}
}
class UnwindSlideDownSegue: UIStoryboardSegue {
override func perform() {
let screenHeight = UIScreen.main.bounds.size.height
let toVC = self.destination
let fromVC = self.source.parent!
fromVC.view.superview?.insertSubview(toVC.view, at: 0)
UIView.animate(withDuration: 0.5, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
fromVC.view.transform = CGAffineTransform(translationX: 0, y: -screenHeight - 100)
}, completion: {
success in
fromVC.dismiss(animated: false, completion: nil)
})
}
}
If I let the unwind do the default where it just leaves by going to to the bottom of the screen but keep my custom for showing the new View, the UINavigationBar
maintains it's correct color, it's only when I used my code provided for unwind that the UINavigationBar
goes black during the animation.
Any hints would be much appreciated.
--- EDIT ---
I played with it a little, if I go into the AppDelegate and change UINavigationBar.appearance().isTranslucent = false
to true, I instead get a white background, but then it just appears that the Navigation Bar is suddenly appearing. I'm wondering if it is for some reason being unloaded and then loaded back in once the View Controller is active.
--- EDIT 2 ---
I am sorta able to fix it with a hack. In the AppDelegate
inside func application(... didFinishLaunchingWithOptions ...)
I added in self.window?.backgroundColor = UIColor.{your color}
but all that does is make that black part now appear my color, the Navigation Bar is still disappearing during the segue for some reason.
This is a perfect opportunity to use snapshotView(afterScreenUpdates:)
. That's how UIKit
performs many of their stock transitions (even in UINavigationController
) and it's a tremendous way to perform custom transitions, particularly if you want to apply effects. Take a snapshot of the destination (which will have an intact navigation bar), insert it right below the source, perform the animation, dismiss the source so that the destination is ready, and finally remove the snapshot which you won't even notice because it will mirror the destination exactly.
如果你想要一个简单的技巧,尽量背景色设置为window
在AppDelegate
的didFinishLaunchingWithOptions
到您的导航栏的颜色,它为我工作:)这样的window?.backgroundColor = myColor
上一篇: 筑巢路线扑动
下一篇: 导航栏在自定义放松Segue期间是黑色的