Add a UIView above all, even the navigation bar

前端 未结 17 1635
悲&欢浪女
悲&欢浪女 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:12

    Swift version of @Nicolas Bonnet 's answer:

        var popupWindow: UIWindow?
    
    func showViewController(controller: UIViewController) {
        self.popupWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
        controller.view.frame = self.popupWindow!.bounds
        self.popupWindow!.rootViewController = controller
        self.popupWindow!.makeKeyAndVisible()
    }
    
    func viewControllerDidRemove() {
        self.popupWindow?.removeFromSuperview()
        self.popupWindow = nil
    }
    

    Don't forget that the window must be a strong property, because the original answer leads to an immediate deallocation of the window

    0 讨论(0)
  • 2020-11-29 16:14

    I recommend you to create a new UIWindow:

    UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    window.rootViewController = viewController;
    window.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    window.opaque = NO;
    window.windowLevel = UIWindowLevelCFShareCircle;
    window.backgroundColor = [UIColor clearColor];
    
    [window makeKeyAndVisible];
    

    Then you can manage your view in an other UIViewController. To remove the windows:

    [window removeFromSuperview];
    window = nil;
    

    hope that will help!

    0 讨论(0)
  • 2020-11-29 16:14
    UIApplication.shared.keyWindow?.insertSubview(yourView, at: 1)
    

    This method works with xcode 9.4 , iOS 11.4

    0 讨论(0)
  • 2020-11-29 16:15

    There is more than one way to do it:

    1- Add your UIView on UIWindow instead of adding it on UIViewController. This way it will be on top of everything.

       [[(AppDelegate *)[UIApplication sharedApplication].delegate window] addSubview:view];
    

    2- Use custom transition that will keep your UIViewController showing in the back with a 0.5 alpha

    For that I recommend you look at this: https://github.com/Citrrus/BlurryModalSegue

    0 讨论(0)
  • 2020-11-29 16:16
    [[UIApplication sharedApplication].windows.lastObject addSubview:myView];
    
    0 讨论(0)
提交回复
热议问题