iOS research Uitabbarcontroller hidden Tabbar and Addchildviewcontroller

Source: Internet
Author: User



My project team recently optimized some of the basic components of the project, and there is a need to summarize some of the questions about Uitabbarcontroller hidden Tabbar.
One, demand analysis
First of all, the basic requirements of the project: The whole project consists of the left column and the main view, the primary view is a uitabbarcontroller, subordinate several nested Uinavigationcontroller Uiviewcontroller.
Requirements: When sliding up and down the page, according to user gestures need to hide the bottom bar, also is the default Uitabbarcontroller tabbar.
When I was designing, I added the left and main view to a container uiviewcontroller by Addchildviewcontroller and used the Tabbar of Uitabbarcontroller. The hidden display Tabbar is implemented by modifying the Tabbar frame. The Interface layout demo code is as follows: Coreanimation not clear enough to see Calayer and catransaction detailed tutorials

Self.window = [[[UIWindow alloc] Initwithframe:[[uiscreen Mainscreen] applicationframe]] autorelease]; Uiviewcontroller *parentcontroller = [[Uiviewcontroller alloc] init]; Sidebarviewcontroller *sidebarviewcontroller = [[Sidebarviewcontroller alloc] init];//left column- It is also necessary to have a pop-up uiviewcontroller function. Uinavigationcontroller *sidebarnavcontroller = [[Uinavigationcontroller alloc] Initwithrootviewcontroller: Sidebarviewcontroller]; Sidebarnavcontroller.navigationbarhidden = YES; [Parentcontroller addchildviewcontroller:sidebarnavcontroller];//Add left sidebar [parentcontroller.view AddSubview: Sidebarnavcontroller.view]; Myuitabbarcontroller *tabbarcontroller = [[Myuitabbarcontroller alloc] init];//body View//[parentcontroller addchildviewcontroller:tabbarcontroller];//added this sentence, the interface has a problem [Parentcontroller.view AddSubview:tabBarController.view]; Self.window.rootViewController = Parentcontroller;
1234567891011 Self.window= [[[UIWindowAlloc]initWithFrame:[[UIScreenMainscreen]Applicationframe]]autorelease];  Uiviewcontroller *Parentcontroller= [[UiviewcontrollerAlloc]Init];  Sidebarviewcontroller *Sidebarviewcontroller= [[SidebarviewcontrollerAlloc]Init];//Left column-also need to have pop-up uiviewcontroller function. Uinavigationcontroller *Sidebarnavcontroller= [[UinavigationcontrollerAlloc]Initwithrootviewcontroller:Sidebarviewcontroller];  Sidebarnavcontroller.Navigationbarhidden= YES;  [ParentcontrollerAddchildviewcontroller:Sidebarnavcontroller];//Add left column[Parentcontroller.ViewAddsubview:Sidebarnavcontroller.View];  Myuitabbarcontroller *Tabbarcontroller= [[MyuitabbarcontrollerAlloc]Init];//Body View//[parentcontroller addchildviewcontroller:tabbarcontroller];//Add this sentence, the interface has a problem[Parentcontroller.ViewAddsubview:Tabbarcontroller.View];   Self.window.Rootviewcontroller= Parentcontroller;

The demo code in Myuitabbarcontroller is as follows:


123456789101112131415 Uiviewcontroller *Controller1= [[UiviewcontrollerAlloc]Init];    Uinavigationcontroller *NavigationController1= [[UinavigationcontrollerAlloc]Initwithrootviewcontroller:Controller1];      Uiviewcontroller *Controller2= [[UiviewcontrollerAlloc]Init];    Uinavigationcontroller *NavigationController2= [[UinavigationcontrollerAlloc]Initwithrootviewcontroller:Controller2];      NavigationController1.Navigationbarhidden= YES;    NavigationController2.Navigationbarhidden= YES;      //controller1.hidesbottombarwhenpushed = YES;   //controller2.hidesbottombarwhenpushed = YES;     Nsarray *Controllers= [Nsarrayarraywithobjects:NavigationController1,NavigationController2,Nil];     Self.viewcontrollers= Controllers;     Self.SelectedIndex= 0;  

In the test: After moving the Tabbar in the iOS SDK6 environment, the interface leaves a blank space, that is, the tabbar part of Uitabbarcontroller is actually exclusive of the area, even if the tabbar is forced to move tabbar through the set frame, Other content is not displayed in this area. On GitHub, there's a solution: Click to view
The program code is simple: After moving the Tabbar, the frame size of the view is modified, and the problem seems to be solved.

Two, why didn't I use Uitabbarcontroller_sethidden
It seems that Uitabbarcontroller_sethidden is able to solve the problem, but when actually using it to find a problem when using Pushviewcontroller pop up a Uiviewcontroller " Configure Hidesbottombarwhenpushed=yes, the page pops up Tabbar hidden, and then popviewcontrolleranimated bounces back, Tabbar automatically displays and sets the frame of the owning page back. In this way, the timing of setting the page frame is not good control, in addition, this method in the page when the switch will have some less beautiful effect.
The benefits of using the system's own Tabbar or Navigationbar are obvious, but in this case (requiring gestures to hide the bottom bar), using a custom Tabbar is not a good deal.
After trying to fail, I decided to use a custom Tabbar and wanted to customize the Tabbar, and one question was how to hide the tabbar that comes with it.

Third, How to hide the Uitabbarcontroller tabbar
There are methods on the network:
Self.tabBar.hidden = YES;
Even the
[Self.tabbar Removefromsuperview];
These can actually implement the hidden Tabbar functionality, but the hidden content is not populated on Tabbar's original location. The correct approach is to:
Make the following settings for each rootviewcontroller in Uitabbarcontroller:
self.homeViewController.hidesBottomBarWhenPushed = YES;
In my project, I also encountered a problem: even if the hidesbottombarwhenpushed is set, at the bottom of the interface there is an area of no content (exactly the area of Tabbar). After the query, found that the reason is to add the main uitabbarcontroller to the container uiviewcontroller, using the Addchildviewcontroller way, will Addchildviewcontroller removed , only keep the Addsubview way, everything works fine.

Four,how can addchildviewcontroller affect Uitabbarcontroller's Tabbar
I don't know what caused the problem for the time being. But here's what I think about Addchildviewcontroller's approach: using Addchildviewcontroller to deal with the scenarios described in this article is not good. Addchildviewcontroller this way I feel mainly to be able to facilitate the page switch (using the Transitionfromviewcontroller method), and in this scenario, is not "two page switch", but the left column and the main view has always existed, Just move away or show the view by hand.
I used to use Addsubview to add the view of the left column and the main view (all Uicontroller view) to a container, and someone on the network said "[Viewcontroller.view Addsubview: Someotherviewcontroller.view]; " This approach is actually a misuse of uiviewcontroller, because doing so "will not trigger the viewwillappear of the controller that is added to view hierarchy's view: Method".

iOS research Uitabbarcontroller hidden Tabbar and Addchildviewcontroller

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.