Search code examples
iosswiftuiviewaddsubviewpresentviewcontroller

UIView which is added as subview in UIWindow is not visible in the presented view controller


I'm showing a UIView as subview in all view controllers using below function.

func showSubViewInAllScreens() {

    let mySubView = MySubView()

    guard let window = UIApplication.shared.windows.last else { return }

    window.addSubview(mySubView)

}

But if I present a new view controller over my current visible view controller, then this subview is not visible in the presented view controller. The subview stays behind the presented view controller. Can anyone please provide a solution to show this subview over all view controllers which are going to be presented after the subview is displayed ?

We can use "window.bringSubviewToFront(mySubView)" to bring the subview over the presented view controller every time a view controller is being presented. But In my case, I can't detect when a view controller is going to be presented.


Solution

  • Increase the z position of mySubView's layer.