Add a UIView above all, even the navigation bar

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

    In Swift 4.2 and Xcode 10

    var spinnerView: UIView? //This is your view
    
    spinnerView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
    //Based on your requirement change width and height like self.view.bounds.size.width
    spinnerView?.backgroundColor = UIColor.black.withAlphaComponent(0.6)
    //        self.view.addSubview(spinnerView)
    let currentWindow: UIWindow? = UIApplication.shared.keyWindow
    currentWindow?.addSubview(spinnerView!)
    

    In Objective C

    UIView *spinnerView;//This is your view

    self.spinnerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height)];  
    //Based on your requirement change width and height like self.view.bounds.size.width
    self.spinnerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    // [self.view addSubview:self.spinnerView];
    UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
    [currentWindow addSubview:self.spinnerView];
    

    This can work either Portrait OR Landscape mode only.

    One more simple code is:

    yourViewName.layer.zPosition = 1//Change you view name
    

提交回复
热议问题