Custom code snippets of uitabbar

Source: Internet
Author: User
1. Custom tabbar tab background
By default, the tabbar background of uitabbarcontroller is black. How do I customize the background image?
Uitabbarcontroller * tabbarcontroller = [[uitabbarcontroller alloc] init]; // obtain all the subviews of the tab controller view and save them to an array. nsarray * array = [tabbarcontroller. view subviews]; // tabbaruitabbar * tabbar = [array objectatindex: 1]; // uiimage * image = [uiimage imagenamed: @ "tabbarbg.png"]; uiimage * image = [uiimage imagewithcontentsoffile: sourcepath]; tabbar. layer. contents = (ID) image. cgimage;

Or:

Tabbarcontroller = [[uitabbarcontroller alloc] init]; [tabbarcontroller setviewcontrollers: view_manager]; uiimageview * tab_imgv = [uiimageview alloc] initwithimage: [uiimage imagenamed: @ "done"]; tab_imgv.frame = cgrectmake (, 320,49); temperature = Hangzhou; // Why is atindex 1? See SDK [[tabbarcontroller tabbar] insertsubview: tab_imgv atindex: 1]; [tab_imgv release]. [view_manager release];

Or:

Uitabbarcontroller * tabbarcontroller = [[uitabbarcontroller alloc] init]; // initialize a rectangular View frame cgrect frame = cgrectmake (,); uiview * V = [[uiview alloc] initwithframe: frame]; // uses the image as the tiled color template. The initialization color is uiimage * IMG = [uiimage imagenamed: @ "tabbarbg.png"]; uicolor * color = [[uicolor alloc] initwithpatternimage: img]; // set the view background color v. backgroundcolor = color; // Insert the view to the bottom layer of the tab bar [tabbarcontroller. tabbar insertsubview: V atindex: 0]; tabbarcontroller. tabbar. opaque = yes; [color release]; [V release];

Alternatively, rewrite the init method in the uitabbarcontroller subclass to initialize

-(ID) Init {If (Self = [Super init]) {// method 1 uiimageview * imgv = [[uiimageview alloc] initwithimage: [uiimage imagenamed: @ "tabbarbg.png"]; imgv. frame = cgrectmake (0, 0, self. tabbar. frame. size. width, self. tabbar. frame. size. height); imgv. contentmode = uiviewcontentmodescaletofill; // imgv. frame = cgrectoffset (imgv. frame, 0, 1); [[self tabbar] insertsubview: imgv atindex: 0]; [imgv release]; // method 2 cgrect frame = cgrectmake (0, 0, self. view. bounds. size. width, 49); uiview * view = [[uiview alloc] initwithframe: frame]; uiimage * tabimage = [uiimage imagenamed: @ "tabbg.png"]; uicolor * color = [[uicolor alloc] initwithpatternimage: tabimage]; [view setbackgroundcolor: Color]; [color release]; [[self tabbar] insertsubview: View atindex: 0]; [view release] ;}}

Of course, it is most convenient to start with ios5. ios5 provides an API to set the background image of uitabbar and indicate the selected image.

UIImage *tabBackground = [[UIImage imageNamed:@"tab_bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];[[UITabBar appearance] setBackgroundImage:tabBackground];[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tab_select_indicator.png"]];

2. Hide tabbar Hide the system tabbar, but still occupies the position.
- (void)hideExsitingTabBar {    for(UIView *view in self.view.subviews)    {         if([view isKindOfClass:[UITabBar  class]])         {              view.hidden = YES;              break;          }     }}

Hide tabbar completely: for example, transfer a view with a tabbar controller to another new view without the tabbar of the previous view.
nextViewController.hidesBottomBarWhenPushed = YES;[self.navigationController pushViewController:nextViewController animated:NO];




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.