So finally I succeeded using the method of Imran to which I added few things to match what I needed.
I have a singleton class for my header ViewController that contains the elements I needed inside of it.
let HEADER = STORYBOARD.instantiateViewController(withIdentifier: "HeaderLayer") as? HeaderViewController
class HeaderViewController: UIViewController {
@IBOutlet weak var ThemeLabel: UILabel!
@IBOutlet weak var DifficultyImage: UIImageView!
@IBOutlet weak var ExitButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// ...
}
// Methods
}
And here's how I put my header in the UIWindow
to make it appear at the top of the screen, where it will stay fixed even during segue animations. I change the frame because in full screen it would disable the interactions for the views underneath.
let STORYBOARD = UIStoryboard(name: "Main", bundle: nil)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
HEADER!.view.frame = CGRect(x: 0, y: 0, width: 1366, height: 142)
window?.addSubview(HEADER!.view)
window?.makeKeyAndVisible()
// ...
}
// ...
}
Then it's necessary to bring the header to front everytime one of my ViewControllers appears.
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let window = UIApplication.shared.keyWindow
window?.bringSubviewToFront(HEADER!.view)
// ...
}
}
I'm not using any UINavigationController
or UITabBarController
, but it is possible with this method.
The only problem I encountered is that UIApplication.shared.keywindow
is deprecated. But it doesn't seem to cause any issue and I didn't find a solution yet.