UIView placement for TabBar & NavBar coverage

I am trying to make a popup ( UIView ) a transparent background (another UIView ). Everything works fine for a "popup UIView ", but I couldn't figure out how to bring a "transparent UIView background" (above the NavigationBar and TabBar).

First I created a UIView in the storyboard and connected the output:

 popupView.center = CGPointMake(CGRectGetMidX(self.view.bounds), tableView.center.y); self.view.addSubview(popupView) popupView.clipsToBounds = true popupView.alpha = 0 

Then, when showing the popupView I create a transparent UIView background:

  func clicked() { self.popupView.alpha = 1 let screenSize: CGRect = UIScreen.mainScreen().bounds let opaqueView = UIView() opaqueView.frame.size = CGSize(width: screenSize.width, height: screenSize.height) opaqueView.backgroundColor = UIColor.blackColor() opaqueView.alpha = 0.5 self.view.addSubview(opaqueView) } 

However, background view does not overlap the NavigationBar or TabBar. I tried this but nothing changed:

 myTabBar.view.bringSubviewToFront(opaqueView) 

What I want to achieve is that having a popup UIView at the very front, having an opaque UIView around everything, including NavBar and TabBar, but behind a popup UIView


Update:

Regarding opaqueView answer, with this snippet I got an opaqueView display via TabBar and NavBar; but now it also goes beyond popupView.

 func display() { popupView.center = CGPointMake(CGRectGetMidX(self.view.bounds), tableView.center.y); self.view.addSubview(popupView) popupView.clipsToBounds = true let opaqueView = UIView() let screenSize: CGRect = UIScreen.mainScreen().bounds opaqueView.frame.size = CGSize(width: screenSize.width, height: screenSize.height) UIApplication.sharedApplication().keyWindow!.insertSubview(opaqueView, belowSubview: popupView) } 

How can I place opaqueView under popupView and opaqueView is above everything else?

+5
source share
3 answers

Try the following:

 UIApplication.sharedApplication().keyWindow!.bringSubviewToFront(opaqueView) 
+5
source
  func addButtonTapped(){ if self.transparentBackground == nil{ self.transparentBackground = UIView(frame: UIScreen.main.bounds) self.transparentBackground.backgroundColor = UIColor(white: 0.0, alpha: 0.54) UIApplication.shared.keyWindow!.addSubview(self.transparentBackground) self.opaqueView = self.setupOpaqueView() self.transparentBackground.addSubview(opaqueView) UIApplication.shared.keyWindow!.bringSubview(toFront: self.transparentBackground) self.view.bringSubview(toFront: transparentBackground) } } func setupOpaqueView() -> UIView{ let mainView = UIView(frame: CGRect(x: 16, y: 100, width: Int(UIScreen.main.bounds.width-32), height: 200)) mainView.backgroundColor = UIColor.white let titleLabel = UILabel(frame: CGRect(x: 16, y: 20, width: Int(mainView.frame.width-32), height: 100)) titleLabel.text = "This is the opaque" titleLabel.textAlignment = .center titleLabel.font = font titleLabel.textColor = UIColor(white: 0.0, alpha: 0.54) mainView.addSubview(titleLabel) let OKbutton = UIButton(frame: CGRect(x: 16, y: Int(mainView.frame.height-60), width: Int(mainView.frame.width-32), height: 45)) OKbutton.backgroundColor = UIColor(red: 40.0 / 255.0, green: 187.0 / 255.0, blue: 187.0 / 255.0, alpha: 1) OKbutton.layer.cornerRadius = 10 mainView.addSubview(OKbutton) OKbutton.setTitle("OK", for: .normal) OKbutton.setTitleColor(UIColor.white, for: .normal) OKbutton.titleLabel?.font = font OKbutton.addTarget(self, action: #selector(FirstViewController.handleOKButtonTapped(_:)), for: .touchUpInside) return mainView } func handleOKButtonTapped(_ sender: UIButton){ UIView.animate(withDuration: 0.3, animations: { self.transparentBackground.alpha = 0 }) { done in self.transparentBackground.removeFromSuperview() self.transparentBackground = nil } } 

result preview

+1
source

The view you want to make transparent use this

yourView.backgroundColor = UIColor.black.withAlphaComponent (0.7)

-1
source

All Articles