Placing UIView To Cover TabBar & NavBar












8















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



First I created the UIView in the Storyboard and connected the outlet:



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


Then, while displaying popupView I am creating the transparent background 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)
}


However, the background view doesn't get over NavigationBar or TabBar. I tried this but nothing changes:



myTabBar.view.bringSubviewToFront(opaqueView)


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





Update:



Regarding @Alex's answer, with this chunk, I achieved displaying opaqueView over TabBar & NavBar; but now it's also going above the 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 below popupView while opaqueView is above everything else?










share|improve this question





























    8















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



    First I created the UIView in the Storyboard and connected the outlet:



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


    Then, while displaying popupView I am creating the transparent background 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)
    }


    However, the background view doesn't get over NavigationBar or TabBar. I tried this but nothing changes:



    myTabBar.view.bringSubviewToFront(opaqueView)


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





    Update:



    Regarding @Alex's answer, with this chunk, I achieved displaying opaqueView over TabBar & NavBar; but now it's also going above the 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 below popupView while opaqueView is above everything else?










    share|improve this question



























      8












      8








      8


      4






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



      First I created the UIView in the Storyboard and connected the outlet:



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


      Then, while displaying popupView I am creating the transparent background 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)
      }


      However, the background view doesn't get over NavigationBar or TabBar. I tried this but nothing changes:



      myTabBar.view.bringSubviewToFront(opaqueView)


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





      Update:



      Regarding @Alex's answer, with this chunk, I achieved displaying opaqueView over TabBar & NavBar; but now it's also going above the 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 below popupView while opaqueView is above everything else?










      share|improve this question
















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



      First I created the UIView in the Storyboard and connected the outlet:



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


      Then, while displaying popupView I am creating the transparent background 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)
      }


      However, the background view doesn't get over NavigationBar or TabBar. I tried this but nothing changes:



      myTabBar.view.bringSubviewToFront(opaqueView)


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





      Update:



      Regarding @Alex's answer, with this chunk, I achieved displaying opaqueView over TabBar & NavBar; but now it's also going above the 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 below popupView while opaqueView is above everything else?







      ios swift uiview addsubview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 31 '16 at 20:12







      senty

















      asked May 31 '16 at 18:06









      sentysenty

      3,432654134




      3,432654134
























          5 Answers
          5






          active

          oldest

          votes


















          10














          Try this:



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


          Updating for Swift 4.2



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





          share|improve this answer


























          • I tried this too, it's bringing the opaqueView over popupView (which I don't want), and doesn't bring it over NavBar and TabBar. Unfortunately, your answer doesn't help :/

            – senty
            May 31 '16 at 18:21








          • 3





            To make it work you should add your opaqueView like this: UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); Then opaque view will be on the top of your subviews. You can't add it to your view (self.view.addSubview(opaqueView)) and wait for displaying it over navigation bar and tab bar. One of other solutions is to add it to tabbarController.view or navigationController.view.

            – Alex Kosyakov
            May 31 '16 at 18:31








          • 1





            Works like charm. Thanks a lot. But I have a question. When I use UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); opaqueView gets over popUpView. I tried using opaqueView.bringSubviewToFront(popupView), but doesn't change anything. How can I bring 'popUpView' above 'opaqueView'?

            – senty
            May 31 '16 at 18:36











          • You can use insertSubview..belowSubview or insertSubview..aboveSubview methods, but in this case you should add your subviews to the same view.

            – Alex Kosyakov
            May 31 '16 at 18:47













          • First I tried UIApplication.sharedApplication().keyWindow!.addSubview(popupView). This didn't change anything. Then, opaqueView.insertSubview(popupView, atIndex: 1), however, it's also making the popupView transparent (opaqueView's 0.5 .alpha affects popupview). I am stuck :/ What can be the solution?

            – senty
            May 31 '16 at 18:53



















          1














              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

          }
          }


          preview of the result






          share|improve this answer


























          • Hey @Hackman this is a great answer! How will you be able to apply a UITapGesture to the transparentBackground to dismiss the view, just the same like the OK button does but instead a UITapGesture. Thank you so much for your response

            – juelizabeth
            Jul 2 '17 at 2:02






          • 1





            Hey @juelizabeth, good question. To have the tap on the transparent view, create a UITapGestureRecognizer and add it to the addButtonTapped method. Just add these two lines to the addButtonTapped let tap = UITapGestureRecognizer(target: self, action: #selector(FirstViewController.handleOKButtonTapped(_:))) self.transparentBackground.addGestureRecognizer(tap)

            – Hackman
            Jul 5 '17 at 11:43













          • Thank you very much, this really helped

            – juelizabeth
            Jul 5 '17 at 14:31











          • what is transparentBackground? the app won't let me to run because the Value of type 'ViewController' has no member 'transparentBackground'

            – Saeed Rahmatolahi
            Jan 6 '18 at 8:01






          • 1





            @SaeedRahmatolahi the transparentBackground is a uiview property. declare inside the view controller

            – Hackman
            Jan 12 '18 at 14:32





















          1














          (swift 3+) This will make your view on top of tab bar and navigation bar



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





          share|improve this answer































            0














            Best solution is:



            tabBarController?.view.addSubview(view)





            share|improve this answer































              -2














              The view which you want to do transparent, use this



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






              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37552739%2fplacing-uiview-to-cover-tabbar-navbar%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                10














                Try this:



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


                Updating for Swift 4.2



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





                share|improve this answer


























                • I tried this too, it's bringing the opaqueView over popupView (which I don't want), and doesn't bring it over NavBar and TabBar. Unfortunately, your answer doesn't help :/

                  – senty
                  May 31 '16 at 18:21








                • 3





                  To make it work you should add your opaqueView like this: UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); Then opaque view will be on the top of your subviews. You can't add it to your view (self.view.addSubview(opaqueView)) and wait for displaying it over navigation bar and tab bar. One of other solutions is to add it to tabbarController.view or navigationController.view.

                  – Alex Kosyakov
                  May 31 '16 at 18:31








                • 1





                  Works like charm. Thanks a lot. But I have a question. When I use UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); opaqueView gets over popUpView. I tried using opaqueView.bringSubviewToFront(popupView), but doesn't change anything. How can I bring 'popUpView' above 'opaqueView'?

                  – senty
                  May 31 '16 at 18:36











                • You can use insertSubview..belowSubview or insertSubview..aboveSubview methods, but in this case you should add your subviews to the same view.

                  – Alex Kosyakov
                  May 31 '16 at 18:47













                • First I tried UIApplication.sharedApplication().keyWindow!.addSubview(popupView). This didn't change anything. Then, opaqueView.insertSubview(popupView, atIndex: 1), however, it's also making the popupView transparent (opaqueView's 0.5 .alpha affects popupview). I am stuck :/ What can be the solution?

                  – senty
                  May 31 '16 at 18:53
















                10














                Try this:



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


                Updating for Swift 4.2



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





                share|improve this answer


























                • I tried this too, it's bringing the opaqueView over popupView (which I don't want), and doesn't bring it over NavBar and TabBar. Unfortunately, your answer doesn't help :/

                  – senty
                  May 31 '16 at 18:21








                • 3





                  To make it work you should add your opaqueView like this: UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); Then opaque view will be on the top of your subviews. You can't add it to your view (self.view.addSubview(opaqueView)) and wait for displaying it over navigation bar and tab bar. One of other solutions is to add it to tabbarController.view or navigationController.view.

                  – Alex Kosyakov
                  May 31 '16 at 18:31








                • 1





                  Works like charm. Thanks a lot. But I have a question. When I use UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); opaqueView gets over popUpView. I tried using opaqueView.bringSubviewToFront(popupView), but doesn't change anything. How can I bring 'popUpView' above 'opaqueView'?

                  – senty
                  May 31 '16 at 18:36











                • You can use insertSubview..belowSubview or insertSubview..aboveSubview methods, but in this case you should add your subviews to the same view.

                  – Alex Kosyakov
                  May 31 '16 at 18:47













                • First I tried UIApplication.sharedApplication().keyWindow!.addSubview(popupView). This didn't change anything. Then, opaqueView.insertSubview(popupView, atIndex: 1), however, it's also making the popupView transparent (opaqueView's 0.5 .alpha affects popupview). I am stuck :/ What can be the solution?

                  – senty
                  May 31 '16 at 18:53














                10












                10








                10







                Try this:



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


                Updating for Swift 4.2



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





                share|improve this answer















                Try this:



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


                Updating for Swift 4.2



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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 2 at 8:04









                Ashutosh Shukla

                54111




                54111










                answered May 31 '16 at 18:18









                Alex KosyakovAlex Kosyakov

                1,38411015




                1,38411015













                • I tried this too, it's bringing the opaqueView over popupView (which I don't want), and doesn't bring it over NavBar and TabBar. Unfortunately, your answer doesn't help :/

                  – senty
                  May 31 '16 at 18:21








                • 3





                  To make it work you should add your opaqueView like this: UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); Then opaque view will be on the top of your subviews. You can't add it to your view (self.view.addSubview(opaqueView)) and wait for displaying it over navigation bar and tab bar. One of other solutions is to add it to tabbarController.view or navigationController.view.

                  – Alex Kosyakov
                  May 31 '16 at 18:31








                • 1





                  Works like charm. Thanks a lot. But I have a question. When I use UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); opaqueView gets over popUpView. I tried using opaqueView.bringSubviewToFront(popupView), but doesn't change anything. How can I bring 'popUpView' above 'opaqueView'?

                  – senty
                  May 31 '16 at 18:36











                • You can use insertSubview..belowSubview or insertSubview..aboveSubview methods, but in this case you should add your subviews to the same view.

                  – Alex Kosyakov
                  May 31 '16 at 18:47













                • First I tried UIApplication.sharedApplication().keyWindow!.addSubview(popupView). This didn't change anything. Then, opaqueView.insertSubview(popupView, atIndex: 1), however, it's also making the popupView transparent (opaqueView's 0.5 .alpha affects popupview). I am stuck :/ What can be the solution?

                  – senty
                  May 31 '16 at 18:53



















                • I tried this too, it's bringing the opaqueView over popupView (which I don't want), and doesn't bring it over NavBar and TabBar. Unfortunately, your answer doesn't help :/

                  – senty
                  May 31 '16 at 18:21








                • 3





                  To make it work you should add your opaqueView like this: UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); Then opaque view will be on the top of your subviews. You can't add it to your view (self.view.addSubview(opaqueView)) and wait for displaying it over navigation bar and tab bar. One of other solutions is to add it to tabbarController.view or navigationController.view.

                  – Alex Kosyakov
                  May 31 '16 at 18:31








                • 1





                  Works like charm. Thanks a lot. But I have a question. When I use UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); opaqueView gets over popUpView. I tried using opaqueView.bringSubviewToFront(popupView), but doesn't change anything. How can I bring 'popUpView' above 'opaqueView'?

                  – senty
                  May 31 '16 at 18:36











                • You can use insertSubview..belowSubview or insertSubview..aboveSubview methods, but in this case you should add your subviews to the same view.

                  – Alex Kosyakov
                  May 31 '16 at 18:47













                • First I tried UIApplication.sharedApplication().keyWindow!.addSubview(popupView). This didn't change anything. Then, opaqueView.insertSubview(popupView, atIndex: 1), however, it's also making the popupView transparent (opaqueView's 0.5 .alpha affects popupview). I am stuck :/ What can be the solution?

                  – senty
                  May 31 '16 at 18:53

















                I tried this too, it's bringing the opaqueView over popupView (which I don't want), and doesn't bring it over NavBar and TabBar. Unfortunately, your answer doesn't help :/

                – senty
                May 31 '16 at 18:21







                I tried this too, it's bringing the opaqueView over popupView (which I don't want), and doesn't bring it over NavBar and TabBar. Unfortunately, your answer doesn't help :/

                – senty
                May 31 '16 at 18:21






                3




                3





                To make it work you should add your opaqueView like this: UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); Then opaque view will be on the top of your subviews. You can't add it to your view (self.view.addSubview(opaqueView)) and wait for displaying it over navigation bar and tab bar. One of other solutions is to add it to tabbarController.view or navigationController.view.

                – Alex Kosyakov
                May 31 '16 at 18:31







                To make it work you should add your opaqueView like this: UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); Then opaque view will be on the top of your subviews. You can't add it to your view (self.view.addSubview(opaqueView)) and wait for displaying it over navigation bar and tab bar. One of other solutions is to add it to tabbarController.view or navigationController.view.

                – Alex Kosyakov
                May 31 '16 at 18:31






                1




                1





                Works like charm. Thanks a lot. But I have a question. When I use UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); opaqueView gets over popUpView. I tried using opaqueView.bringSubviewToFront(popupView), but doesn't change anything. How can I bring 'popUpView' above 'opaqueView'?

                – senty
                May 31 '16 at 18:36





                Works like charm. Thanks a lot. But I have a question. When I use UIApplication.sharedApplication().keyWindow!.addSubview(opaqueView); opaqueView gets over popUpView. I tried using opaqueView.bringSubviewToFront(popupView), but doesn't change anything. How can I bring 'popUpView' above 'opaqueView'?

                – senty
                May 31 '16 at 18:36













                You can use insertSubview..belowSubview or insertSubview..aboveSubview methods, but in this case you should add your subviews to the same view.

                – Alex Kosyakov
                May 31 '16 at 18:47







                You can use insertSubview..belowSubview or insertSubview..aboveSubview methods, but in this case you should add your subviews to the same view.

                – Alex Kosyakov
                May 31 '16 at 18:47















                First I tried UIApplication.sharedApplication().keyWindow!.addSubview(popupView). This didn't change anything. Then, opaqueView.insertSubview(popupView, atIndex: 1), however, it's also making the popupView transparent (opaqueView's 0.5 .alpha affects popupview). I am stuck :/ What can be the solution?

                – senty
                May 31 '16 at 18:53





                First I tried UIApplication.sharedApplication().keyWindow!.addSubview(popupView). This didn't change anything. Then, opaqueView.insertSubview(popupView, atIndex: 1), however, it's also making the popupView transparent (opaqueView's 0.5 .alpha affects popupview). I am stuck :/ What can be the solution?

                – senty
                May 31 '16 at 18:53













                1














                    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

                }
                }


                preview of the result






                share|improve this answer


























                • Hey @Hackman this is a great answer! How will you be able to apply a UITapGesture to the transparentBackground to dismiss the view, just the same like the OK button does but instead a UITapGesture. Thank you so much for your response

                  – juelizabeth
                  Jul 2 '17 at 2:02






                • 1





                  Hey @juelizabeth, good question. To have the tap on the transparent view, create a UITapGestureRecognizer and add it to the addButtonTapped method. Just add these two lines to the addButtonTapped let tap = UITapGestureRecognizer(target: self, action: #selector(FirstViewController.handleOKButtonTapped(_:))) self.transparentBackground.addGestureRecognizer(tap)

                  – Hackman
                  Jul 5 '17 at 11:43













                • Thank you very much, this really helped

                  – juelizabeth
                  Jul 5 '17 at 14:31











                • what is transparentBackground? the app won't let me to run because the Value of type 'ViewController' has no member 'transparentBackground'

                  – Saeed Rahmatolahi
                  Jan 6 '18 at 8:01






                • 1





                  @SaeedRahmatolahi the transparentBackground is a uiview property. declare inside the view controller

                  – Hackman
                  Jan 12 '18 at 14:32


















                1














                    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

                }
                }


                preview of the result






                share|improve this answer


























                • Hey @Hackman this is a great answer! How will you be able to apply a UITapGesture to the transparentBackground to dismiss the view, just the same like the OK button does but instead a UITapGesture. Thank you so much for your response

                  – juelizabeth
                  Jul 2 '17 at 2:02






                • 1





                  Hey @juelizabeth, good question. To have the tap on the transparent view, create a UITapGestureRecognizer and add it to the addButtonTapped method. Just add these two lines to the addButtonTapped let tap = UITapGestureRecognizer(target: self, action: #selector(FirstViewController.handleOKButtonTapped(_:))) self.transparentBackground.addGestureRecognizer(tap)

                  – Hackman
                  Jul 5 '17 at 11:43













                • Thank you very much, this really helped

                  – juelizabeth
                  Jul 5 '17 at 14:31











                • what is transparentBackground? the app won't let me to run because the Value of type 'ViewController' has no member 'transparentBackground'

                  – Saeed Rahmatolahi
                  Jan 6 '18 at 8:01






                • 1





                  @SaeedRahmatolahi the transparentBackground is a uiview property. declare inside the view controller

                  – Hackman
                  Jan 12 '18 at 14:32
















                1












                1








                1







                    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

                }
                }


                preview of the result






                share|improve this answer















                    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

                }
                }


                preview of the result







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jun 4 '17 at 7:42

























                answered Jun 4 '17 at 7:30









                HackmanHackman

                10117




                10117













                • Hey @Hackman this is a great answer! How will you be able to apply a UITapGesture to the transparentBackground to dismiss the view, just the same like the OK button does but instead a UITapGesture. Thank you so much for your response

                  – juelizabeth
                  Jul 2 '17 at 2:02






                • 1





                  Hey @juelizabeth, good question. To have the tap on the transparent view, create a UITapGestureRecognizer and add it to the addButtonTapped method. Just add these two lines to the addButtonTapped let tap = UITapGestureRecognizer(target: self, action: #selector(FirstViewController.handleOKButtonTapped(_:))) self.transparentBackground.addGestureRecognizer(tap)

                  – Hackman
                  Jul 5 '17 at 11:43













                • Thank you very much, this really helped

                  – juelizabeth
                  Jul 5 '17 at 14:31











                • what is transparentBackground? the app won't let me to run because the Value of type 'ViewController' has no member 'transparentBackground'

                  – Saeed Rahmatolahi
                  Jan 6 '18 at 8:01






                • 1





                  @SaeedRahmatolahi the transparentBackground is a uiview property. declare inside the view controller

                  – Hackman
                  Jan 12 '18 at 14:32





















                • Hey @Hackman this is a great answer! How will you be able to apply a UITapGesture to the transparentBackground to dismiss the view, just the same like the OK button does but instead a UITapGesture. Thank you so much for your response

                  – juelizabeth
                  Jul 2 '17 at 2:02






                • 1





                  Hey @juelizabeth, good question. To have the tap on the transparent view, create a UITapGestureRecognizer and add it to the addButtonTapped method. Just add these two lines to the addButtonTapped let tap = UITapGestureRecognizer(target: self, action: #selector(FirstViewController.handleOKButtonTapped(_:))) self.transparentBackground.addGestureRecognizer(tap)

                  – Hackman
                  Jul 5 '17 at 11:43













                • Thank you very much, this really helped

                  – juelizabeth
                  Jul 5 '17 at 14:31











                • what is transparentBackground? the app won't let me to run because the Value of type 'ViewController' has no member 'transparentBackground'

                  – Saeed Rahmatolahi
                  Jan 6 '18 at 8:01






                • 1





                  @SaeedRahmatolahi the transparentBackground is a uiview property. declare inside the view controller

                  – Hackman
                  Jan 12 '18 at 14:32



















                Hey @Hackman this is a great answer! How will you be able to apply a UITapGesture to the transparentBackground to dismiss the view, just the same like the OK button does but instead a UITapGesture. Thank you so much for your response

                – juelizabeth
                Jul 2 '17 at 2:02





                Hey @Hackman this is a great answer! How will you be able to apply a UITapGesture to the transparentBackground to dismiss the view, just the same like the OK button does but instead a UITapGesture. Thank you so much for your response

                – juelizabeth
                Jul 2 '17 at 2:02




                1




                1





                Hey @juelizabeth, good question. To have the tap on the transparent view, create a UITapGestureRecognizer and add it to the addButtonTapped method. Just add these two lines to the addButtonTapped let tap = UITapGestureRecognizer(target: self, action: #selector(FirstViewController.handleOKButtonTapped(_:))) self.transparentBackground.addGestureRecognizer(tap)

                – Hackman
                Jul 5 '17 at 11:43







                Hey @juelizabeth, good question. To have the tap on the transparent view, create a UITapGestureRecognizer and add it to the addButtonTapped method. Just add these two lines to the addButtonTapped let tap = UITapGestureRecognizer(target: self, action: #selector(FirstViewController.handleOKButtonTapped(_:))) self.transparentBackground.addGestureRecognizer(tap)

                – Hackman
                Jul 5 '17 at 11:43















                Thank you very much, this really helped

                – juelizabeth
                Jul 5 '17 at 14:31





                Thank you very much, this really helped

                – juelizabeth
                Jul 5 '17 at 14:31













                what is transparentBackground? the app won't let me to run because the Value of type 'ViewController' has no member 'transparentBackground'

                – Saeed Rahmatolahi
                Jan 6 '18 at 8:01





                what is transparentBackground? the app won't let me to run because the Value of type 'ViewController' has no member 'transparentBackground'

                – Saeed Rahmatolahi
                Jan 6 '18 at 8:01




                1




                1





                @SaeedRahmatolahi the transparentBackground is a uiview property. declare inside the view controller

                – Hackman
                Jan 12 '18 at 14:32







                @SaeedRahmatolahi the transparentBackground is a uiview property. declare inside the view controller

                – Hackman
                Jan 12 '18 at 14:32













                1














                (swift 3+) This will make your view on top of tab bar and navigation bar



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





                share|improve this answer




























                  1














                  (swift 3+) This will make your view on top of tab bar and navigation bar



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





                  share|improve this answer


























                    1












                    1








                    1







                    (swift 3+) This will make your view on top of tab bar and navigation bar



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





                    share|improve this answer













                    (swift 3+) This will make your view on top of tab bar and navigation bar



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






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 21 '18 at 14:54









                    Daniel RaoufDaniel Raouf

                    802617




                    802617























                        0














                        Best solution is:



                        tabBarController?.view.addSubview(view)





                        share|improve this answer




























                          0














                          Best solution is:



                          tabBarController?.view.addSubview(view)





                          share|improve this answer


























                            0












                            0








                            0







                            Best solution is:



                            tabBarController?.view.addSubview(view)





                            share|improve this answer













                            Best solution is:



                            tabBarController?.view.addSubview(view)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 5 at 18:15









                            DanielDaniel

                            17826




                            17826























                                -2














                                The view which you want to do transparent, use this



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






                                share|improve this answer




























                                  -2














                                  The view which you want to do transparent, use this



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






                                  share|improve this answer


























                                    -2












                                    -2








                                    -2







                                    The view which you want to do transparent, use this



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






                                    share|improve this answer













                                    The view which you want to do transparent, use this



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







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jun 6 '17 at 10:32









                                    sumeetsumeet

                                    9714




                                    9714






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37552739%2fplacing-uiview-to-cover-tabbar-navbar%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        MongoDB - Not Authorized To Execute Command

                                        in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                        Npm cannot find a required file even through it is in the searched directory