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.
If you want a simple trick ,try to set background color to window
in AppDelegate
's didFinishLaunchingWithOptions
to your navigationbar's color, it works for me :)
like this
window?.backgroundColor = myColor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With