Imitation Sina Weibo iOS client (v5.2.8)--Custom Uitabbar Replace system default (add "+" sign button)

Source: Internet
Author: User

Reprint please indicate source: http://blog.csdn.net/android_ls/article/details/45896395

Disclaimer: Sina Weibo project, the use of all the image resources are derived from the official Sina Weibo iOS client, the purpose of writing this application is to learn communication, such as infringement please inform, I will promptly replace the relevant pictures used.


Custom Uitabbar replaces the system by default, in order to add a "+ sign" button in the middle of the Uitabbar, let's talk about the specific implementation.

1, custom Wbtabbar, let it inherit from Uitabbar, the code is as follows:

  wbtabbar.h//  sinaweibo////  Created by Android_ls on 15/5/21.//  Copyright (c) 2015 Android_ls. All rights reserved.//#import <UIKit/UIKit.h> @interface wbtabbar:uitabbar@end

2, Tabbar is a read-only member variable (attribute) of Uitabbarcontroller, is not allowed to modify, the declaration in the UITabBarController.h file is as follows:

@property (nonatomic,readonly) Uitabbar *tabbar Ns_available_ios (3_0);
needle For this case, we can use KVC way, replace the system comes with the Uitabbar, the implementation code is as follows:

    Wbtabbar *tabbar = [[Wbtabbar alloc] init];    [Self Setvalue:tabbar forkeypath:@ "TabBar"];

3, add a UIButton to Wbtabbar, the implementation code is as follows:

-(ID) initWithFrame: (cgrect) frame{self = [super Initwithframe:frame];        if (self) {//Add a button to Tabbar UIButton *plusbtn = [[UIButton alloc] init];        [Plusbtn setbackgroundimage:[uiimage imagenamed:@ "Tabbar_compose_button"] forstate:uicontrolstatenormal]; [Plusbtn setbackgroundimage:[uiimage imagenamed:@ "tabbar_compose_button_highlighted"] forState:        Uicontrolstatehighlighted];        [Plusbtn setimage:[uiimage imagenamed:@ "Tabbar_compose_background_icon_add"] forstate:uicontrolstatenormal]; [Plusbtn setimage:[uiimage imagenamed:@ "tabbar_compose_icon_add_highlighted"] forstate:uicontrolstatehighlighted]        ;        Plusbtn.size = plusBtn.currentBackgroundImage.size;        [Plusbtn addtarget:self Action: @selector (Plusclick) forcontrolevents:uicontroleventtouchupinside];        [Self addsubview:plusbtn];    SELF.PLUSBTN = plusbtn; } return self;}

4, set the position of the plus button, adjust the position and width of each uitabbarbutton in the Wbtabbar, the specific implementation code is as follows:
-(void) layoutsubviews{    [Super layoutsubviews];        1. Set the position of the plus button    Self.plusBtn.centerX = self.width * 0.5;    Self.plusBtn.centerY = self.height * 0.5;        2. Set the location and size of other Uitabbarbutton    cgfloat tabbarbuttonw = SELF.WIDTH/5;    CGFloat tabbarbuttonindex = 0;    For (UIView *child in self.subviews) {        Class class = Nsclassfromstring (@ "Uitabbarbutton");        if ([child iskindofclass:class]) {            //set width            child.width = tabbarbuttonw;            Set x            child.x = Tabbarbuttonindex * TABBARBUTTONW;                        Increase index            tabbarbuttonindex++;            if (Tabbarbuttonindex = = 2) {                tabbarbuttonindex++            ;        }    }}}

5, define the Wbtabbardelegate protocol, declare the agent of Wbtabbar, the code is as follows:
  wbtabbar.h//  sinaweibo////  Created by Android_ls on 15/5/21.//  Copyright (c) 2015 Android_ls. All rights reserved.//#import <UIKit/UIKit.h> #pragma mark has already declared a uitabbardelegate agreement in Uitabbar, #pragma mark If we want to add an external proxy function, we can let our custom protocol inherit from Uitabbardelegate, adding an extension function. @class Wbtabbar; @protocol wbtabbardelegate <UITabBarDelegate> @optional-(void) Tabbardidclickplusbutton: ( Wbtabbar *) TabBar; @end @interface Wbtabbar:uitabbar@property (nonatomic, weak) id<wbtabbardelegate> Tabbardelegate; @end

6. In the Click event handler of the Plus button, notify the agent
#pragma the Mark Plus button click event handler-(void) plusclick{    //Notification Agent    if ([Self.tabbardelegate respondstoselector: @selector ( Tabbardidclickplusbutton:)] {        [self.tabbardelegate tabbardidclickplusbutton:self];    }}

7, in the Wbtabbarcontroller set Wbtabbar agent, the specific implementation of the following:

2, the use of KVC way, replace the system comes with the Uitabbar    wbtabbar *tabbar = [[Wbtabbar alloc] init];    Tabbar.tabbardelegate = self;    [Self Setvalue:tabbar forkeypath:@ "TabBar"];

#pragma mark-hwtabbardelegate proxy Method-(void) Tabbardidclickplusbutton: (Wbtabbar *) tabbar{    Composeviewcontroller * composeviewcontroller= [[Composeviewcontroller alloc] init];    Uinavigationcontroller * Navigationcontroller = [[Uinavigationcontroller alloc]initwithrootviewcontroller: Composeviewcontroller];    [Self Presentviewcontroller:navigationcontroller animated:yes completion:nil];}

8, there is a picture of the truth, has been achieved as follows:



Click the plus button to pop up the writing screen as follows:


WBTABBARCONTROLLER.M file complete source code is as follows:

wbtabbarcontroller.m//sinaweibo////Created by Android_ls on 15/5/17.//Copyright (c) 2015 Android_ls. All rights reserved.//#import "WBTabBarController.h" #import "HomeViewController.h" #import "MessageViewController.h" #import "DiscoverViewController.h" #import "ProfileViewController.h" #import "WBNavigationController.h" #import "    WBTabBar.h "#import" ComposeViewController.h "@interface Wbtabbarcontroller () <wbtabbardelegate>{    Homeviewcontroller * _homeviewcontroller;    Messageviewcontroller * _messageviewcontroller;    Discoverviewcontroller * _discoverviewcontroller; Profileviewcontroller * _profileviewcontroller;}        @end @implementation wbtabbarcontroller-(void) viewdidload {[Super viewdidload];    Initialize the child controller and add it to uitabbarcontroller _homeviewcontroller = [[Homeviewcontroller alloc]init];        [Self addchildcontroller:_homeviewcontroller title:@ "Home" image:@ "Tabbar_home"];    _messageviewcontroller = [[Messageviewcontroller alloc]init]; [Self AddchildconTroller:_messageviewcontroller title:@ "message" image:@ "Tabbar_message_center"];    _discoverviewcontroller = [[Discoverviewcontroller alloc]init];        [Self addchildcontroller:_discoverviewcontroller title:@ "found" image:@ "Tabbar_discover"];    _profileviewcontroller = [[Profileviewcontroller alloc]init];        [Self Addchildcontroller:_profileviewcontroller title:@ "i" image:@ "Tabbar_profile"];    @property (nonatomic,readonly) Uitabbar *tabbar Ns_available_ios (3_0); TabBar is a read-only member variable (attribute) of Uitabbarcontroller, is not allowed to modify//2, using KVC Way, replace the system comes with uitabbar wbtabbar *tabbar = [[Wbtabbar alloc    ] init];    Tabbar.tabbardelegate = self;    [Self Setvalue:tabbar forkeypath:@ "TabBar"]; } #pragma mark-hwtabbardelegate proxy Method-(void) Tabbardidclickplusbutton: (Wbtabbar *) tabbar{Composeviewcontroller *    composeviewcontroller= [[Composeviewcontroller alloc] init]; Uinavigationcontroller * Navigationcontroller = [[Uinavigationcontroller alloc]initwithrootviewcontroller: Composeviewcontroller]; [Self Presentviewcontroller:navigationcontroller animated:yes completion:nil];} /** * Add sub-controller to Uitabbarcontroller */-(void) Addchildcontroller: (Uiviewcontroller *) Childviewcontroller title: (        NSString *) Title Image: (NSString *) image{//Set sub-controller, Tabbar and navigationbar title Childviewcontroller.title = title;    Set the default indication picture on Tabbaritem and the picture when selected ChildViewController.tabBarItem.image = [UIImage imagenamed:image]; ChildViewController.tabBarItem.selectedImage = [[UIImage imagenamed:[nsstring stringwithformat:@ "%@%@", Image, @ "_        Selected "]]imagewithrenderingmode:uiimagerenderingmodealwaysoriginal"; Set the style of text on Tabbaritem (here is the color value for setting text in different states) [Childviewcontroller.tabbaritem settitletextattributes: @{nsforegroundcol    Orattributename:kcolor (117, 117, 117)} Forstate:uicontrolstatenormal]; [Childviewcontroller.tabbaritem settitletextattributes: @{nsforegroundcolorattributename:kcolor (253, 109, 10)}        Forstate:uicontrolstateselected]; Using the system default Uinavigationcontroller[Self Addchildviewcontroller:[[uinavigationcontroller alloc] Initwithrootviewcontroller:childviewcontroller]; Use our custom navigation bar (Wbnavigationcontroller inherits from Uinavigationcontroller) Wbnavigationcontroller * Navigationcontroller = [WB    Navigationcontroller Alloc]initwithrootviewcontroller:childviewcontroller]; [Self addchildviewcontroller:navigationcontroller];} @end

WBTabBar.h file Complete source code is as follows:

  wbtabbar.h//  sinaweibo////  Created by Android_ls on 15/5/21.//  Copyright (c) 2015 Android_ls. All rights reserved.//#import <UIKit/UIKit.h> #pragma mark has already declared a uitabbardelegate agreement in Uitabbar, #pragma mark If we want to add an external proxy function, we can let our custom protocol inherit from Uitabbardelegate, adding an extension function. @class Wbtabbar; @protocol wbtabbardelegate <UITabBarDelegate> @optional-(void) Tabbardidclickplusbutton: ( Wbtabbar *) TabBar; @end @interface Wbtabbar:uitabbar@property (nonatomic, weak) id<wbtabbardelegate> Tabbardelegate; @end

wbtabbar.m file Complete source code is as follows:

wbtabbar.m//sinaweibo////Created by Android_ls on 15/5/21.//Copyright (c) 2015 Android_ls. All rights reserved.//#import "WBTabBar.h" @interface Wbtabbar () @property (nonatomic, weak) UIButton *plusbtn;@    End@implementation wbtabbar-(ID) initWithFrame: (cgrect) frame{self = [super Initwithframe:frame];        if (self) {//Add a button to Tabbar UIButton *plusbtn = [[UIButton alloc] init];        [Plusbtn setbackgroundimage:[uiimage imagenamed:@ "Tabbar_compose_button"] forstate:uicontrolstatenormal]; [Plusbtn setbackgroundimage:[uiimage imagenamed:@ "tabbar_compose_button_highlighted"] forState:        Uicontrolstatehighlighted];        [Plusbtn setimage:[uiimage imagenamed:@ "Tabbar_compose_background_icon_add"] forstate:uicontrolstatenormal]; [Plusbtn setimage:[uiimage imagenamed:@ "tabbar_compose_icon_add_highlighted"] forstate:uicontrolstatehighlighted]        ;        Plusbtn.size = plusBtn.currentBackgroundImage.size; [Plusbtn addtarget:self Action: @selector (PLUSClick) Forcontrolevents:uicontroleventtouchupinside];        [Self addsubview:plusbtn];    SELF.PLUSBTN = plusbtn; } return self;} #pragma the Mark Plus button click event handler-(void) plusclick{//Notification proxy if ([Self.tabbardelegate respondstoselector: @selector (TABBARDIDCL    (Ickplusbutton:)]) {[Self.tabbardelegate tabbardidclickplusbutton:self];        }}-(void) layoutsubviews{[Super Layoutsubviews];    1. Set the position of the plus button Self.plusBtn.centerX = self.width * 0.5;        Self.plusBtn.centerY = self.height * 0.5;    2. Set the location and size of other Uitabbarbutton cgfloat tabbarbuttonw = SELF.WIDTH/5;    CGFloat tabbarbuttonindex = 0;        For (UIView *child in self.subviews) {Class class = Nsclassfromstring (@ "Uitabbarbutton");            if ([child iskindofclass:class]) {//Set width child.width = tabbarbuttonw;                        Set x child.x = Tabbarbuttonindex * TABBARBUTTONW;            Increase index tabbarbuttonindex++; if (Tabbarbuttonindex = = 2){tabbarbuttonindex++; }}} @end

Composeviewcontroller inherits from Uiviewcontroller, mainly implemented in M files, whose complete code is as follows:

  composeviewcontroller.m//  sinaweibo////  Created by Android_ls on 15/5/21.//  Copyright (c) 2015 Android_ls. All rights reserved.//#import "ComposeViewController.h" #import "Uibarbuttonitem+category.h" @interface Composeviewcontroller () @end @implementation composeviewcontroller-(void) viewdidload {    [super viewdidload];    Self.title = @ "Writing interface";    Self.view.backgroundColor = [Uicolor whitecolor];        Self.navigationItem.leftBarButtonItem = [Uibarbuttonitem itemwithtarget:self                            Action: @selector (back)                            image : @ "Navigationbar_back_withtext"                            highimage:@ "navigationbar_back_withtext_highlighted"                            title:@ "return"];} -(void) back{    [Self.navigationcontroller dismissviewcontrolleranimated:yes completion:^{}];} @end

As a programmer, I'm not a good talker. Many times to interview, some of the knowledge points I used, but also know the principle, but is not clear, old people despise.

Finally talk about my technical blog writing ideas, the first detailed explanation of each step of the implementation, and finally I will post the complete code, for the readers who want to read carefully to see a happy.


Imitation Sina Weibo iOS client (v5.2.8)--Custom Uitabbar Replace system default (add "+" sign button)

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.