I'm developing an app for tvOS in Swift using UIKit
and I would like to implement a black cross-fade segue between two ViewControllers that stands for the whole functionality and does not require any custom views or code added to the source and destination ViewControllers.
What I'm looking for is something like this:
I did search a lot and tried many things but had little luck in getting it to work. Two complications make it all the more difficult:
ViewControllers
are contained inside a UISearchController
.I would appreciate any help I can get.
I finally found a decent way of doing it. Here's a custom segue class that seamlessly performs the transition.
import UIKit
import Foundation
class StoryboardFadeToBlackSegue: UIStoryboardSegue {
override func perform() {
guard let window = (UIApplication.shared.delegate as? AppDelegate)?.window else {
super.perform()
return
}
let overlay = UIView(frame: window.frame)
overlay.layer.zPosition = 1
overlay.backgroundColor = .black
overlay.alpha = 0.0
window.addSubview(overlay)
UIView.animate(withDuration: 0.5, delay: 0, options: [.curveEaseOut], animations: {
overlay.alpha = 1.0
}, completion: { _ in
self.source.present(self.destination, animated: false, completion: {
UIView.animate(withDuration: 0.6, delay: 0, options: [.curveEaseIn], animations: {
overlay.alpha = 0.0
}, completion: { _ in
overlay.removeFromSuperview()
})
})
})
}
}
In order to use it, set this class as the associated Class in the attributes inspector for your Segue.