放置UIView来覆盖TabBar和NavBar

sen*_*nty 11 uiview addsubview ios swift

我正在尝试UIView使用透明背景(另一个UIView)制作一个popup().对于'弹出窗口UIView' 一切都工作正常,但我无法弄清楚如何带来'透明背景UIView'(在NavigationBar和TabBar之上).

首先,我在故事板中创建了UIView并连接了插座:

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

然后,在显示popupView我正在创建透明背景UIView:

 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)
 }
Run Code Online (Sandbox Code Playgroud)

但是,后台视图不会覆盖NavigationBar或TabBar.我尝试了这个,但没有改变:

myTabBar.view.bringSubviewToFront(opaqueView)
Run Code Online (Sandbox Code Playgroud)

我想要实现的是,虽然popup UIView在最前面,拥有opaque UIView包括NavBar和TabBar在内的所有东西,但却落伍popup UIView


更新:

关于@Alex的回答,有了这个块,我实现opaqueView了TabBar和NavBar的显示; 但现在它也超越了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) 
}
Run Code Online (Sandbox Code Playgroud)

当opaqueView高于其他所有内容时,如何将opaqueView放在popupView下面?

Ale*_*kov 11

试试这个:

UIApplication.sharedApplication().keyWindow!.bringSubviewToFront(opaqueView)
Run Code Online (Sandbox Code Playgroud)

更新Swift 4.2

 UIApplication.shared.keyWindow!.bringSubview(toFront: opaqueView!)
Run Code Online (Sandbox Code Playgroud)

  • 为了使它工作,你应该像这样添加你的opaqueView:UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); 然后,不透明视图将位于子视图的顶部.您无法将其添加到视图(self.view.addSubview(opaqueView))并等待在导航栏和标签栏上显示它.其他解决方案之一是将其添加到tabbarController.view或navigationController.view. (5认同)

F. *_*les 8

UIApplication.shared.keyWindow!.bringSubviewToFront(view: view) 在 iOS 13 后被弃用。让透明视图覆盖选项卡控制器和导航控制器的最佳方法就是这样做。

if let tabBarController = self.tabBarController {
    tabBarController.view.addSubview(view)
}
Run Code Online (Sandbox Code Playgroud)


Hac*_*man 5

    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

              }
          }
Run Code Online (Sandbox Code Playgroud)

结果预览


Dan*_*ouf 5

(swift 3+) 这将使您的视图位于标签栏和导航栏的顶部

UIApplication.shared.keyWindow!.addSubview(yourView)
UIApplication.shared.keyWindow!.bringSubview(toFront: yourView)
Run Code Online (Sandbox Code Playgroud)


Dan*_*iel 5

最好的解决方案是:

tabBarController?.view.addSubview(view)
Run Code Online (Sandbox Code Playgroud)