How to add a view behind other view in iOS










43















I want to add a view under my tab bar analog in iOS and then show it with animation coming from behind it. But when I use my code, the view that must come from behind my tab bar overlaps my tab bar for a while.



tab bar and a view that shows when tab bar button pressed



This is my code:



- (void)handlePressedButton 
if (pressedButton.selected)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform1 = CGAffineTransformMakeTranslation(-196.0, 0.0);
CGAffineTransform transform2 = CGAffineTransformMakeTranslation(0.0, 0.0);
[leftPanelView setTransform:transform1];
[movedView setTransform:transform2];
[UIView commitAnimations];
pressedButton.selected = NO;

else
pressedButton.selected = YES;
if (leftPanelView)
[leftPanelView removeFromSuperview];
leftPanelView = nil;

CGRect viewFrame = CGRectMake(-196, 0, 236, 748);
leftPanelView = [[UIView alloc] initWithFrame:viewFrame];
leftPanelView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"left-nav-content.png"]];

// Code to populate leftPanelView according to what button is pressed.
[self populateLeftPanelView];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.view addSubview:leftPanelView];
CGAffineTransform transform1 = CGAffineTransformMakeTranslation(236.0, 0.0);
CGAffineTransform transform2 = CGAffineTransformMakeTranslation(112.0, 0.0);
[leftPanelView setTransform:transform1];
[movedView setTransform:transform2];
[UIView commitAnimations];





Here the leftPanelView is the view that must come under a tab bar. Moved view is another view, that is at the right of left panel (don't mind it)










share|improve this question






















  • Check if this works : [self.navigationController.view insertSubview: <subview> belowSubview:self.navigationController.navigationBar];

    – Jayprakash Dubey
    May 6 '16 at 6:20
















43















I want to add a view under my tab bar analog in iOS and then show it with animation coming from behind it. But when I use my code, the view that must come from behind my tab bar overlaps my tab bar for a while.



tab bar and a view that shows when tab bar button pressed



This is my code:



- (void)handlePressedButton 
if (pressedButton.selected)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform1 = CGAffineTransformMakeTranslation(-196.0, 0.0);
CGAffineTransform transform2 = CGAffineTransformMakeTranslation(0.0, 0.0);
[leftPanelView setTransform:transform1];
[movedView setTransform:transform2];
[UIView commitAnimations];
pressedButton.selected = NO;

else
pressedButton.selected = YES;
if (leftPanelView)
[leftPanelView removeFromSuperview];
leftPanelView = nil;

CGRect viewFrame = CGRectMake(-196, 0, 236, 748);
leftPanelView = [[UIView alloc] initWithFrame:viewFrame];
leftPanelView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"left-nav-content.png"]];

// Code to populate leftPanelView according to what button is pressed.
[self populateLeftPanelView];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.view addSubview:leftPanelView];
CGAffineTransform transform1 = CGAffineTransformMakeTranslation(236.0, 0.0);
CGAffineTransform transform2 = CGAffineTransformMakeTranslation(112.0, 0.0);
[leftPanelView setTransform:transform1];
[movedView setTransform:transform2];
[UIView commitAnimations];





Here the leftPanelView is the view that must come under a tab bar. Moved view is another view, that is at the right of left panel (don't mind it)










share|improve this question






















  • Check if this works : [self.navigationController.view insertSubview: <subview> belowSubview:self.navigationController.navigationBar];

    – Jayprakash Dubey
    May 6 '16 at 6:20














43












43








43


8






I want to add a view under my tab bar analog in iOS and then show it with animation coming from behind it. But when I use my code, the view that must come from behind my tab bar overlaps my tab bar for a while.



tab bar and a view that shows when tab bar button pressed



This is my code:



- (void)handlePressedButton 
if (pressedButton.selected)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform1 = CGAffineTransformMakeTranslation(-196.0, 0.0);
CGAffineTransform transform2 = CGAffineTransformMakeTranslation(0.0, 0.0);
[leftPanelView setTransform:transform1];
[movedView setTransform:transform2];
[UIView commitAnimations];
pressedButton.selected = NO;

else
pressedButton.selected = YES;
if (leftPanelView)
[leftPanelView removeFromSuperview];
leftPanelView = nil;

CGRect viewFrame = CGRectMake(-196, 0, 236, 748);
leftPanelView = [[UIView alloc] initWithFrame:viewFrame];
leftPanelView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"left-nav-content.png"]];

// Code to populate leftPanelView according to what button is pressed.
[self populateLeftPanelView];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.view addSubview:leftPanelView];
CGAffineTransform transform1 = CGAffineTransformMakeTranslation(236.0, 0.0);
CGAffineTransform transform2 = CGAffineTransformMakeTranslation(112.0, 0.0);
[leftPanelView setTransform:transform1];
[movedView setTransform:transform2];
[UIView commitAnimations];





Here the leftPanelView is the view that must come under a tab bar. Moved view is another view, that is at the right of left panel (don't mind it)










share|improve this question














I want to add a view under my tab bar analog in iOS and then show it with animation coming from behind it. But when I use my code, the view that must come from behind my tab bar overlaps my tab bar for a while.



tab bar and a view that shows when tab bar button pressed



This is my code:



- (void)handlePressedButton 
if (pressedButton.selected)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform1 = CGAffineTransformMakeTranslation(-196.0, 0.0);
CGAffineTransform transform2 = CGAffineTransformMakeTranslation(0.0, 0.0);
[leftPanelView setTransform:transform1];
[movedView setTransform:transform2];
[UIView commitAnimations];
pressedButton.selected = NO;

else
pressedButton.selected = YES;
if (leftPanelView)
[leftPanelView removeFromSuperview];
leftPanelView = nil;

CGRect viewFrame = CGRectMake(-196, 0, 236, 748);
leftPanelView = [[UIView alloc] initWithFrame:viewFrame];
leftPanelView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"left-nav-content.png"]];

// Code to populate leftPanelView according to what button is pressed.
[self populateLeftPanelView];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.view addSubview:leftPanelView];
CGAffineTransform transform1 = CGAffineTransformMakeTranslation(236.0, 0.0);
CGAffineTransform transform2 = CGAffineTransformMakeTranslation(112.0, 0.0);
[leftPanelView setTransform:transform1];
[movedView setTransform:transform2];
[UIView commitAnimations];





Here the leftPanelView is the view that must come under a tab bar. Moved view is another view, that is at the right of left panel (don't mind it)







iphone ios view core-animation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 20 '12 at 15:43









wzbozonwzbozon

8,78145860




8,78145860












  • Check if this works : [self.navigationController.view insertSubview: <subview> belowSubview:self.navigationController.navigationBar];

    – Jayprakash Dubey
    May 6 '16 at 6:20


















  • Check if this works : [self.navigationController.view insertSubview: <subview> belowSubview:self.navigationController.navigationBar];

    – Jayprakash Dubey
    May 6 '16 at 6:20

















Check if this works : [self.navigationController.view insertSubview: <subview> belowSubview:self.navigationController.navigationBar];

– Jayprakash Dubey
May 6 '16 at 6:20






Check if this works : [self.navigationController.view insertSubview: <subview> belowSubview:self.navigationController.navigationBar];

– Jayprakash Dubey
May 6 '16 at 6:20













5 Answers
5






active

oldest

votes


















48














try sendSubviewToBack



[self.view sendSubviewToBack:leftPanelView];





share|improve this answer























  • I accept your answer, coz you've sent it after 2 mins, I posted. I think it's fair.

    – wzbozon
    Apr 9 '12 at 17:06






  • 10





    This does not answer the question asked. All this does is move a view to the back of the subview chain. The answer below from He Shiming is correct.

    – Rickster
    Apr 7 '15 at 18:03











  • Thanks for the answer. It worked for me.

    – Ramakrishna
    Feb 6 '17 at 7:15


















117














Besides addSubview, there are other methods you can rely on to add your view:



– insertSubview:aboveSubview:
– insertSubview:belowSubview:
– insertSubview:atIndex:


You can control the index (actually z-index, or layer order) of the view using these methods.






share|improve this answer


















  • 1





    For me insertSubview:belowSubview: solved the problem! Thx

    – Alex Cio
    Oct 22 '14 at 12:31






  • 1





    This should be the right answer. the above answer doesn't answer what was asked.

    – MarkosDarkin
    Oct 25 '17 at 12:41


















5














Oh, I found a solution myself. I added after this:



[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.view addSubview:leftPanelView];


this line:



[self.view sendSubviewToBack:leftPanelView];





share|improve this answer






























    5














    Swift



    Insert a view below another view in the view hierarchy:



    view.insertSubview(subview1, belowSubview: subview2)


    Insert a view above another view in the view hierarchy:



    view.insertSubview(subview1, aboveSubview: subview2)


    Insert a subview at the specified index:



    view.insertSubview(subview, at: index)



    Move the specified subview so that it appears behind its siblings:



    subview1.sendSubviewToBack(subview2)


    Move the specified subview so that it appears on top of its siblings:



    subview1.bringSubviewToFront(subview2)



    Exchange the subviews at the specified indices:



    view.exchangeSubview(at: index1, withSubviewAt: index2)





    share|improve this answer
































      1














      In Swift 4.2



      self.view.sendSubviewToBack(leftPanelView)





      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%2f9790040%2fhow-to-add-a-view-behind-other-view-in-ios%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









        48














        try sendSubviewToBack



        [self.view sendSubviewToBack:leftPanelView];





        share|improve this answer























        • I accept your answer, coz you've sent it after 2 mins, I posted. I think it's fair.

          – wzbozon
          Apr 9 '12 at 17:06






        • 10





          This does not answer the question asked. All this does is move a view to the back of the subview chain. The answer below from He Shiming is correct.

          – Rickster
          Apr 7 '15 at 18:03











        • Thanks for the answer. It worked for me.

          – Ramakrishna
          Feb 6 '17 at 7:15















        48














        try sendSubviewToBack



        [self.view sendSubviewToBack:leftPanelView];





        share|improve this answer























        • I accept your answer, coz you've sent it after 2 mins, I posted. I think it's fair.

          – wzbozon
          Apr 9 '12 at 17:06






        • 10





          This does not answer the question asked. All this does is move a view to the back of the subview chain. The answer below from He Shiming is correct.

          – Rickster
          Apr 7 '15 at 18:03











        • Thanks for the answer. It worked for me.

          – Ramakrishna
          Feb 6 '17 at 7:15













        48












        48








        48







        try sendSubviewToBack



        [self.view sendSubviewToBack:leftPanelView];





        share|improve this answer













        try sendSubviewToBack



        [self.view sendSubviewToBack:leftPanelView];






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 20 '12 at 16:01









        aderader

        5,24411626




        5,24411626












        • I accept your answer, coz you've sent it after 2 mins, I posted. I think it's fair.

          – wzbozon
          Apr 9 '12 at 17:06






        • 10





          This does not answer the question asked. All this does is move a view to the back of the subview chain. The answer below from He Shiming is correct.

          – Rickster
          Apr 7 '15 at 18:03











        • Thanks for the answer. It worked for me.

          – Ramakrishna
          Feb 6 '17 at 7:15

















        • I accept your answer, coz you've sent it after 2 mins, I posted. I think it's fair.

          – wzbozon
          Apr 9 '12 at 17:06






        • 10





          This does not answer the question asked. All this does is move a view to the back of the subview chain. The answer below from He Shiming is correct.

          – Rickster
          Apr 7 '15 at 18:03











        • Thanks for the answer. It worked for me.

          – Ramakrishna
          Feb 6 '17 at 7:15
















        I accept your answer, coz you've sent it after 2 mins, I posted. I think it's fair.

        – wzbozon
        Apr 9 '12 at 17:06





        I accept your answer, coz you've sent it after 2 mins, I posted. I think it's fair.

        – wzbozon
        Apr 9 '12 at 17:06




        10




        10





        This does not answer the question asked. All this does is move a view to the back of the subview chain. The answer below from He Shiming is correct.

        – Rickster
        Apr 7 '15 at 18:03





        This does not answer the question asked. All this does is move a view to the back of the subview chain. The answer below from He Shiming is correct.

        – Rickster
        Apr 7 '15 at 18:03













        Thanks for the answer. It worked for me.

        – Ramakrishna
        Feb 6 '17 at 7:15





        Thanks for the answer. It worked for me.

        – Ramakrishna
        Feb 6 '17 at 7:15













        117














        Besides addSubview, there are other methods you can rely on to add your view:



        – insertSubview:aboveSubview:
        – insertSubview:belowSubview:
        – insertSubview:atIndex:


        You can control the index (actually z-index, or layer order) of the view using these methods.






        share|improve this answer


















        • 1





          For me insertSubview:belowSubview: solved the problem! Thx

          – Alex Cio
          Oct 22 '14 at 12:31






        • 1





          This should be the right answer. the above answer doesn't answer what was asked.

          – MarkosDarkin
          Oct 25 '17 at 12:41















        117














        Besides addSubview, there are other methods you can rely on to add your view:



        – insertSubview:aboveSubview:
        – insertSubview:belowSubview:
        – insertSubview:atIndex:


        You can control the index (actually z-index, or layer order) of the view using these methods.






        share|improve this answer


















        • 1





          For me insertSubview:belowSubview: solved the problem! Thx

          – Alex Cio
          Oct 22 '14 at 12:31






        • 1





          This should be the right answer. the above answer doesn't answer what was asked.

          – MarkosDarkin
          Oct 25 '17 at 12:41













        117












        117








        117







        Besides addSubview, there are other methods you can rely on to add your view:



        – insertSubview:aboveSubview:
        – insertSubview:belowSubview:
        – insertSubview:atIndex:


        You can control the index (actually z-index, or layer order) of the view using these methods.






        share|improve this answer













        Besides addSubview, there are other methods you can rely on to add your view:



        – insertSubview:aboveSubview:
        – insertSubview:belowSubview:
        – insertSubview:atIndex:


        You can control the index (actually z-index, or layer order) of the view using these methods.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 20 '12 at 16:03









        He ShimingHe Shiming

        4,33932653




        4,33932653







        • 1





          For me insertSubview:belowSubview: solved the problem! Thx

          – Alex Cio
          Oct 22 '14 at 12:31






        • 1





          This should be the right answer. the above answer doesn't answer what was asked.

          – MarkosDarkin
          Oct 25 '17 at 12:41












        • 1





          For me insertSubview:belowSubview: solved the problem! Thx

          – Alex Cio
          Oct 22 '14 at 12:31






        • 1





          This should be the right answer. the above answer doesn't answer what was asked.

          – MarkosDarkin
          Oct 25 '17 at 12:41







        1




        1





        For me insertSubview:belowSubview: solved the problem! Thx

        – Alex Cio
        Oct 22 '14 at 12:31





        For me insertSubview:belowSubview: solved the problem! Thx

        – Alex Cio
        Oct 22 '14 at 12:31




        1




        1





        This should be the right answer. the above answer doesn't answer what was asked.

        – MarkosDarkin
        Oct 25 '17 at 12:41





        This should be the right answer. the above answer doesn't answer what was asked.

        – MarkosDarkin
        Oct 25 '17 at 12:41











        5














        Oh, I found a solution myself. I added after this:



        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [self.view addSubview:leftPanelView];


        this line:



        [self.view sendSubviewToBack:leftPanelView];





        share|improve this answer



























          5














          Oh, I found a solution myself. I added after this:



          [UIView beginAnimations:nil context:nil];
          [UIView setAnimationDuration:0.5];
          [self.view addSubview:leftPanelView];


          this line:



          [self.view sendSubviewToBack:leftPanelView];





          share|improve this answer

























            5












            5








            5







            Oh, I found a solution myself. I added after this:



            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.5];
            [self.view addSubview:leftPanelView];


            this line:



            [self.view sendSubviewToBack:leftPanelView];





            share|improve this answer













            Oh, I found a solution myself. I added after this:



            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.5];
            [self.view addSubview:leftPanelView];


            this line:



            [self.view sendSubviewToBack:leftPanelView];






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 20 '12 at 15:59









            wzbozonwzbozon

            8,78145860




            8,78145860





















                5














                Swift



                Insert a view below another view in the view hierarchy:



                view.insertSubview(subview1, belowSubview: subview2)


                Insert a view above another view in the view hierarchy:



                view.insertSubview(subview1, aboveSubview: subview2)


                Insert a subview at the specified index:



                view.insertSubview(subview, at: index)



                Move the specified subview so that it appears behind its siblings:



                subview1.sendSubviewToBack(subview2)


                Move the specified subview so that it appears on top of its siblings:



                subview1.bringSubviewToFront(subview2)



                Exchange the subviews at the specified indices:



                view.exchangeSubview(at: index1, withSubviewAt: index2)





                share|improve this answer





























                  5














                  Swift



                  Insert a view below another view in the view hierarchy:



                  view.insertSubview(subview1, belowSubview: subview2)


                  Insert a view above another view in the view hierarchy:



                  view.insertSubview(subview1, aboveSubview: subview2)


                  Insert a subview at the specified index:



                  view.insertSubview(subview, at: index)



                  Move the specified subview so that it appears behind its siblings:



                  subview1.sendSubviewToBack(subview2)


                  Move the specified subview so that it appears on top of its siblings:



                  subview1.bringSubviewToFront(subview2)



                  Exchange the subviews at the specified indices:



                  view.exchangeSubview(at: index1, withSubviewAt: index2)





                  share|improve this answer



























                    5












                    5








                    5







                    Swift



                    Insert a view below another view in the view hierarchy:



                    view.insertSubview(subview1, belowSubview: subview2)


                    Insert a view above another view in the view hierarchy:



                    view.insertSubview(subview1, aboveSubview: subview2)


                    Insert a subview at the specified index:



                    view.insertSubview(subview, at: index)



                    Move the specified subview so that it appears behind its siblings:



                    subview1.sendSubviewToBack(subview2)


                    Move the specified subview so that it appears on top of its siblings:



                    subview1.bringSubviewToFront(subview2)



                    Exchange the subviews at the specified indices:



                    view.exchangeSubview(at: index1, withSubviewAt: index2)





                    share|improve this answer















                    Swift



                    Insert a view below another view in the view hierarchy:



                    view.insertSubview(subview1, belowSubview: subview2)


                    Insert a view above another view in the view hierarchy:



                    view.insertSubview(subview1, aboveSubview: subview2)


                    Insert a subview at the specified index:



                    view.insertSubview(subview, at: index)



                    Move the specified subview so that it appears behind its siblings:



                    subview1.sendSubviewToBack(subview2)


                    Move the specified subview so that it appears on top of its siblings:



                    subview1.bringSubviewToFront(subview2)



                    Exchange the subviews at the specified indices:



                    view.exchangeSubview(at: index1, withSubviewAt: index2)






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 2 '18 at 18:45

























                    answered Jul 4 '18 at 20:05









                    xmasalovxmasalov

                    13135




                    13135





















                        1














                        In Swift 4.2



                        self.view.sendSubviewToBack(leftPanelView)





                        share|improve this answer



























                          1














                          In Swift 4.2



                          self.view.sendSubviewToBack(leftPanelView)





                          share|improve this answer

























                            1












                            1








                            1







                            In Swift 4.2



                            self.view.sendSubviewToBack(leftPanelView)





                            share|improve this answer













                            In Swift 4.2



                            self.view.sendSubviewToBack(leftPanelView)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 14 '18 at 13:17









                            Vinoth VinoVinoth Vino

                            3,04332636




                            3,04332636



























                                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%2f9790040%2fhow-to-add-a-view-behind-other-view-in-ios%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







                                這個網誌中的熱門文章

                                How to read a connectionString WITH PROVIDER in .NET Core?

                                In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                                Museum of Modern and Contemporary Art of Trento and Rovereto