开发者社区> 问答> 正文

IOS-当视图控制器以模式显示时,我如何保持前面的视图?

我有一个导航视图控制器,我想添加一个名为TestView当用户关闭互联网时,在所有ViewController上。当Internet连接状态发生变化时,我将向keyWindow添加自定义视图。当我推送ViewController时,视图将显示并停留在那里,但当我以模态方式呈现ViewController时,MODERViewController将覆盖该视图。

我希望视图保持在前面,即使VC是以模式方式呈现的,并且我希望能够在我的AppDelegate文件中为所有的模态视图控制器添加一些代码,或者诸如此类的东西。我怎么能在神速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
    }

}

主视图控制器:

互联网关闭:

推送视图控制器:

当前视图控制器:

展开
收起
游客5akardh5cojhg 2019-12-23 20:57:00 563 0
1 条回答
写回答
取消 提交回答
  • 我可以建议一种方法:

    若要打开更高级别的新UIWindow,请将其绘制在整个应用程序之上。

    我不知道这是否是最好的方法,但是我只是在一个空的应用程序上用下面的代码来测试它--而且它看起来很有效--甚至可以是透明的。

    _myWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; 
     [_myWindow makeKeyAndVisible];
    
     UIViewController* dummyVC = [[UIViewController alloc] init];
     dummyVC.view = [[UIView alloc]initWithFrame:_myWindow.bounds];
     [dummyVC.view setOpaque:NO]; 
     [dummyVC.view setAlpha:0.1f];
    
     [_myWindow setRootViewController:dummyVC];
     _myWindow.windowLevel = UIWindowLevelAlert + 1;
    
     [_myWindow.rootViewController.view.layer setBackgroundColor:[UIColor greenColor].CGColor];
    
    2019-12-23 20:57:33
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载