【问题标题】:Placing UIView To Cover TabBar & NavBar放置 UIView 覆盖 TabBar 和 NavBar
【发布时间】:2016-09-29 21:09:37
【问题描述】:

我正在尝试使用透明背景(另一个UIView)制作一个弹出窗口(@98​​7654321@)。 “弹出窗口UIView”一切正常,但我不知道如何带来“透明背景UIView”(在NavigationBar 和TabBar 上方)。

首先我在 Storyboard 中创建了 UIView 并连接了插座:

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

然后,在显示 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)
 }

但是,背景视图不会超过 NavigationBar 或 TabBar。我试过了,但没有任何改变:

myTabBar.view.bringSubviewToFront(opaqueView)

我想要实现的是,popup UIView 位于最前面,opaque UIView 位于包括 NavBar 和 TabBar 在内的所有内容之上,但位于 popup UIView 之后


更新:

关于@Alex 的回答,通过这个块,我实现了在 TabBar 和 NavBar 上显示 opaqueView;但现在它也在 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) 
}

如何将 opaqueView 置于 popupView 下方,而 opaqueView 高于其他一切?

【问题讨论】:

    标签: ios swift uiview addsubview


    【解决方案1】:

    试试这个:

    UIApplication.sharedApplication().keyWindow!.bringSubviewToFront(opaqueView)
    

    为 Swift 4.2 更新

     UIApplication.shared.keyWindow!.bringSubview(toFront: opaqueView!)
    

    为 Swift 5 更新

    UIApplication.shared.keyWindow!.bringSubviewToFront(opaqueView!)
    

    【讨论】:

    • 我也试过这个,它把 opaqueView 带到了 popupView(我不想要),并没有把它带到 NavBar 和 TabBar。不幸的是,你的回答没有帮助:/
    • 为了让它工作,你应该像这样添加你的 opaqueView: UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView);然后不透明视图将位于子视图的顶部。您不能将其添加到您的视图 (self.view.addSubview(opaqueView)) 并等待在导航栏和标签栏上显示它。其他解决方案之一是将其添加到 tabbarController.view 或 navigationController.view。
    • 像魅力一样工作。非常感谢。但我有一个问题。当我使用UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); opaqueView 时,会通过 popUpView。我尝试使用opaqueView.bringSubviewToFront(popupView),但没有改变任何东西。如何将“popUpView”置于“opaqueView”之上?
    • 您可以使用 insertSubview..belowSubview 或 insertSubview..aboveSubview 方法,但在这种情况下,您应该将子视图添加到同一个视图中。
    • 首先我尝试了UIApplication.sharedApplication().keyWindow!.addSubview(popupView)。这并没有改变什么。然后,opaqueView.insertSubview(popupView, atIndex: 1),然而,它也使 popupView 透明(opaqueView 的 0.5 .alpha 影响 popupview)。我被卡住了:/ 有什么解决办法?
    【解决方案2】:
        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
    
                  }
              }
    

    【讨论】:

    • 嘿@Hackman,这是一个很好的答案!您将如何将 UITapGesture 应用到 transparentBackground 以关闭视图,就像 OK 按钮一样,而是使用 UITapGesture。非常感谢您的回复
    • 嘿@juelizabeth,好问题。要在透明视图上点击,请创建一个 UITapGestureRecognizer 并将其添加到 addButtonTapped 方法。只需将这两行添加到 addButtonTapped let tap = UITapGestureRecognizer(target: self, action: #selector(FirstViewController.handleOKButtonTapped(_:))) self.transparentBackground.addGestureRecognizer(tap)
    • 非常感谢,这真的很有帮助
    • 什么是透明背景?该应用程序不会让我运行,因为“ViewController”类型的值没有成员“transparentBackground”
    • @SaeedRahmatolahi transparentBackground 是一个 uiview 属性。在视图控制器中声明
    【解决方案3】:

    要透明的视图,用这个

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

    【讨论】:

      【解决方案4】:

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

      UIApplication.shared.keyWindow!.addSubview(yourView)
      UIApplication.shared.keyWindow!.bringSubview(toFront: yourView)
      

      【讨论】:

        【解决方案5】:

        最好的解决办法是:

        tabBarController?.view.addSubview(view)
        

        【讨论】:

          【解决方案6】:

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

          if let tabBarController = self.tabBarController {
              tabBarController.view.addSubview(view)
          }
          

          【讨论】:

            猜你喜欢
            • 2020-07-12
            • 1970-01-01
            • 1970-01-01
            • 2013-11-13
            • 1970-01-01
            • 2019-11-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多