[iOS Weibo project-1.6]-Custom Tabbar

Source: Internet
Author: User

A. Custom Tabbar1. Requirements control the text color of item within the Tabbar (normal, selected, and icon), background (normal, selected, transparent) reset the item position within the Tabbar, and prepare for the next step to add a "+" button to the middle of tabbar GitHub: Https://github.com/hellovoidworld/HVWWeibo system Default Style:
    • Item font color is blue when checked
After the completion of the style: 2. Thinking package Tabbar, inherit Uitabbar, Override the Position setting method for Tabbar internal controls using KVC to reset the Tabbar members in Uitabbarcontroller (because Uitabbarcontroller in Tabbar is read-only, KVC can modify _ directly TabBar) Modify the Uitabbar Item property to modify the font (normal, selected) 3. Implementation Exploration (1) Modify the font color setting of item Uitabbar agent, listen for item check event, change item's Font property
1 -(void) viewdidappear: (BOOL) Animated {2     nsmutabledictionary *attr = [ Nsmutabledictionary dictionary]; 3     Attr[nsforegroundcolorattributename] = [Uicolor orangecolor]; 4    5      for inch self.tabBar.items) {6        [item settitletextattributes:attr forstate: Uicontrolstateselected]; 7     }8 }

#mark: Note that Tabbaritem is equivalent to model data, Tabbar is the View (2) Customizing an integrated Uitabbar class for encapsulating custom Tabbar
    • Encapsulates the code above to change the color of the Tabbarbutton text
    • Rewrite the location dimension of the Tabbarbutton, and place a "+" button in the middle of the empty position
1 //2 //HVWTABBAR.M3 //Hvwweibo4 //5 //Created by Hellovoidworld on 15/2/3.6 //Copyright (c) 2015 Hellovoidworld. All rights reserved.7 //8 9 #import "HVWTabBar.h"Ten  One @implementationHvwtabbar A  -- (void) Layoutsubviews { -     //Remember to call the method of the parent class!!! the [Super Layoutsubviews]; -     -     //Set Text Properties - [self inittextattr]; +     -     //Setting the location of the Barbutton + [self initbarbuttonposition]; A     at     //Add a "+" button - [self Addcomposebutton]; - } -  - /** Set Text properties*/ -- (void) inittextattr { inNsmutabledictionary *attr =[Nsmutabledictionary dictionary]; -Attr[nsforegroundcolorattributename] =[Uicolor Orangecolor]; to     +      for(Uitabbaritem *iteminchself.items) { -         //Set Font Color the [item settitletextattributes:attr forstate:uicontrolstateselected]; *     } $ }Panax Notoginseng  - /** Set the location of the Barbutton*/ the- (void) initbarbuttonposition { +     A     //Create a location so that it is used to locate the     intindex =0; +     -      for(UIView *tabbarbuttoninchself.subviews) { $         if([Tabbarbutton iskindofclass:nsclassfromstring (@"Uitabbarbutton")]) { $             //Calculate dimensions, reserve a "+" space -CGFloat width = self.width/(Self.items.count +1); -Tabbarbutton.width =width; the             -             //Calculate LocationWuyi             if(Index < (int) (Self.items.count/2)) { thetabbarbutton.x = width *index; -}Else { Wutabbarbutton.x = width * (index +1); -             } About             $index++; -         } -     } - } A  + /** Add "+" button*/ the- (void) Addcomposebutton { -     //Initialize button $UIButton *plusbutton =[UIButton Buttonwithtype:uibuttontypecustom]; the[Plusbutton setbackgroundimage:[uiimage imagewithnamed:@"Tabbar_compose_button"] forstate:uicontrolstatenormal]; the[Plusbutton setbackgroundimage:[uiimage imagewithnamed:@"tabbar_compose_button_highlighted"] forstate:uicontrolstatehighlighted]; the[Plusbutton setimage:[uiimage imagewithnamed:@"Tabbar_compose_icon_add"] forstate:uicontrolstatenormal]; the[Plusbutton setimage:[uiimage imagewithnamed:@"tabbar_compose_icon_add_highlighted"] forstate:uicontrolstatehighlighted]; -     in     //Set Position size theCGFloat width = self.width/(Self.items.count +1); theCGFloat height =Self.height; AboutCGFloat x = (Self.items.count/2) *width; theCGFloat y =0; thePlusbutton.frame =CGRectMake (x, y, width, height); the     +     //Add to Tabbar - [self Addsubview:plusbutton]; the }Bayi  the @end

(3) "+" button click event
    • Pop up a new screen to write a new Weibo
    • Create a new directory "compose" is responsible for the micro-blogging business
    • Create a hvwcomposeviewcontroller of an integrated Uiviewcontroller
1 //2 //HVWCOMPOSEVIEWCONTROLLER.M3 //Hvwweibo4 //5 //Created by Hellovoidworld on 15/2/3.6 //Copyright (c) 2015 Hellovoidworld. All rights reserved.7 //8 9 #import "HVWComposeViewController.h"Ten  One @interfaceHvwcomposeviewcontroller () A  - @end -  the @implementationHvwcomposeviewcontroller -  -- (void) Viewdidload { - [Super Viewdidload]; +     //Do any additional setup after loading the view. -     +     //Initialize some of the function buttons ASelf.view.backgroundColor =[Uicolor Redcolor]; atSelf.title =@"+ number Eject controller"; -     -Self.navigationItem.leftBarButtonItem = [[Uibarbuttonitem alloc] Initwithtitle:@"Exit"Style:uibarbuttonitemstyleplain target:self Action: @selector (dismiss)]; - } -  -- (void) Dismiss { in [self dismissviewcontrolleranimated:yes completion:nil]; -     to } +  - @end

Click on the "+" method:
1 //HVWTABBARVIEWCONTROLLER.M2 #pragmaMark-hvwtabbardelegate3 /** "+" button click Proxy Method*/4- (void) Tabbardidcomposebuttonclick: (Hvwtabbar *) TabBar {5Hvwcomposeviewcontroller *composeview =[[Hvwcomposeviewcontroller alloc] init];6  7     //Tabbarcontroller not from Navigationcontroller, no Navigationcontroller .8 //[Self.navigationcontroller PUSHVIEWCONTROLLER:VC animated:yes];9 //Hvwlog (@ "%@", Self.navigationcontroller);//NULLTen     One     //to use the navigation bar, use Navigationcontroller to wrap AHvwnavigationviewcontroller *nav =[[Hvwnavigationviewcontroller alloc] initwithrootviewcontroller:composeview]; -     //eject using modal mode - [self Presentviewcontroller:nav animated:yes completion:nil]; the}

[iOS Weibo project-1.6]-Custom Tabbar

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.