Add a UIView above all, even the navigation bar

前端 未结 17 1672
悲&欢浪女
悲&欢浪女 2020-11-29 15:36

I want to display, above any other views, even the navigation bar, a kind of \"pop-up\" view that looks like this:

  • full screen black background with a 0.5 alph
17条回答
  •  执念已碎
    2020-11-29 16:06

    You can do that by adding your view directly to the keyWindow:

    UIView *myView = /* <- Your custom view */;
    UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
    [currentWindow addSubview:myView];
    

    UPDATE -- For Swift 4.1 and above

    let currentWindow: UIWindow? = UIApplication.shared.keyWindow
    currentWindow?.addSubview(myView)
    

    UPDATE for iOS13 and above

    keyWindow is deprecated. You should use the following:

    UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.addSubview(myView)
    

提交回复
热议问题