iOS- 以模态显示viewcontroller时如何将视图保持在最前面?

问题描述 投票:0回答:1

我有一个导航ViewController,当用户关闭Internet时,我想在所有ViewController上添加一个名为TestView的自定义视图。当互联网连接状态更改时,我将自定义视图添加到keyWindow中。当我按下ViewController时,视图将显示并停留在该位置,但是当我以模态显示ViewController时,模态ViewController会覆盖该视图。

即使模式以VC形式呈现,我也希望视图保持在最前面,我希望能够在我的AppDelegate文件中为所有模式ViewController或类似的东西添加一些代码。如何在Swift 5中做到这一点?我在想,是否每次模态VC出现时都会触发一个函数,我可以在其中添加代码。这是我的AppDelegate当前的样子:


class AppDelegate: UIResponder, UIApplicationDelegate, ReachabilityObserverDelegate   {
    var reachability: Reachability?
    var window: UIWindow?
    var internetView: TestView?

    //This is called when the internet connects or disconnects
    func reachabilityChanged(_ isReachable: Bool) { 
        window = getKEYWindow()
        if !isReachable {
            window?.addSubview(internetView!)
        }
        else {
            internetView?.removeFromSuperview()
        }
    }

    func getKEYWindow() -> UIWindow? {
        return UIApplication.shared.connectedScenes.filter({$0.activationState == .foregroundActive}).map({$0 as? UIWindowScene}).compactMap({$0}).first?.windows.filter({$0.isKeyWindow}).first
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        try? addReachabilityObserver()
        internetView = TestView(frame: CGRect(x:0, y: 50, width: UIScreen.main.bounds.size.width, height: 14))
        return true
    }

}

Main ViewController:

Internet Turned Off:

Push ViewController:

Present ViewController Modally:

ios swift xib modalviewcontroller presentmodalviewcontroller
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.