Uitabbarcontroller is a very common class used in iOS app development, inheriting from Uiviewcontroller, to enable switching between multiple views, but many times the system's own Tabbar does not meet our needs. The system comes with some properties that we can not modify, like switching the size of the picture, this time we need to customize the Tabbar. For example, if we want to achieve this effect of the tabbar below, we will not be able to do it using the system.
Show the picture you need http://download.csdn.net/detail/zsmile123/8136531
------------------------------------------------------The following demonstration process-----------------------------------------------
First, create a new view controller that inherits from Uiviewcontroller, optionally named Customtarbarviewcontroller, tick the xib. Let's first lay out the Tabbar, which is the tabbar of the simulation system.
-----------------------------drag and Drop View,imageview, and 5 buttons, and set the tag value to 5 buttons 4,5------------------------------------------------
----------------------------------------------------------------------------------------------------
----------------------------------------the CustomTarBarViewController.h file--------------------------------------------
#import <UIKit/UIKit.h>
@interface customtarbarviewcontroller:uiviewcontroller<uinavigationcontrollerdelegate>
{
//image array in normal state
Nsarray *_nomalimagearray;
An array of images in the highlighted state
Nsarray *_hightlightedimagearray;
}
Add two attributes, SelectedIndex is used to record the selected view (or button), the viewcontrollers array is used to hold the button click on the corresponding view controller
@property (nonatomic , assign)nsinteger selectedindex;
@property (nonatomic , retain)nsarray *viewcontrollers;
Connect the view in the Xib, and the 5 button
@property (Retain, nonatomic) iboutlet UIView *tabbarview;
-(ibaction) Tabbarbuttonclick: (UIButton *) sender;
@end
-----------------------------------------------------------the customtarbarviewcontroller.m file------------------------------- -----
-(ID) initwithnibname: (NSString *) Nibnameornil Bundle: (nsbundle *) nibbundleornil
{
Self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];
if (self) {
This is the first view that can be loaded each time you first enter.
_selectedindex =-1;
}
return self;
}
-(void) Viewdidload
{
[Super Viewdidload];
Self . View. backgroundcolor = [Uicolor redcolor];
//Button picture in normal state
_nomalimagearray = [[nsarray alloc] initwithobjects: @ "Tabbar_button_binders_ Normal.png ", @" Tabbar_button_updates_normal.png ", @" Tabbar_button_centeradd.png ", @" Tabbar_button_ Sessions_normal.png ", @" Tabbar_button_notes_normal.png ",Nil];
Picture of the button in the highlighted state
_hightlightedimagearray = [[nsarray alloc]initwithobjects: @ "Tabbar_button_binders_ Selected.png ", @" Tabbar_button_updates_selected.png ", @" Tabbar_button_centeradd.png ", @" Tabbar_ Button_sessions_selected.png ", @" Tabbar_button_notes_selected.png ",Nil];
if (_selectedindex = =-1)
{
Self . SelectedIndex = 0;
}
}
switch view options #pragma mark----
-(ibaction) Tabbarbuttonclick: (UIButton *) Sender
{
The currently selected button Self.selectedindex calls the set method, and the button's tag value-1 is its corresponding view
Self . SelectedIndex = sender. tag- 1;
}
#pragma mark---- override The Set method of the SelectedIndex property
-(void) setSelectedIndex: (nsinteger) Aselectedindex
{
//To determine if the new value is equal to the original value, the selection is still the current view, do not handle
if (Aselectedindex = = _selectedindex)
{
return;
}
_selectedindex >=0 Note A button is selected
if (_selectedindex >= 0)
{
need to remove the previous view
Remove the previously selected view from the View controller array according to _selectedindex
Uiviewcontroller *previousviewcontroller = [_viewcontrollers objectatindex:_selectedindex];
[Previousviewcontroller. view Removefromsuperview];
//need to change the picture of the previous button to a normal state
UIButton *previousbutton = (UIButton *) [self. View viewwithtag:_selectedindex+1];
[Previousbutton setbackgroundimage: [UIImage imagenamed: [_nomalimagearray Objectatindex:_selectedindex]] forstate:(uicontrolstatenormal)];
}
and assign the new Aselectedindex to _selectedindex .
_selectedindex = Aselectedindex;
Show the new View
Uiviewcontroller *currentviewcontroller = [_viewcontrollers objectatindex:_selectedindex];
Locate the current button and change its background image to highlight
UIButton *currentbutton = (UIButton *) [self. View Viewwithtag:_selectedindex + 1];
[Currentbutton setbackgroundimage: [UIImage imagenamed: [_hightlightedimagearray Objectatindex:_selectedindex]] forstate:(uicontrolstatenormal)];
//Because the first view is added to the navigation, you need to determine
if ([Currentviewcontroller iskindofclass:[Uinavigationcontroller class]])
{
indicates that this view is Navgationcontroller
set up a proxy for navigation
((Uinavigationcontroller *) currentviewcontroller). delegate = self ;
}
//Set the size of the current view
Currentviewcontroller. view. frame = CGRectMake (0, 0, . View. bounds. size. height-);
Add to Tab view
[Self. view Addsubview:currentviewcontroller. View];
[Self. view Sendsubviewtoback:currentviewcontroller. View];
}
@end
Add a view to Tabbar--------------------------------------the AppDelegate.h file------------------------------------------------
#import "AppDelegate.h"
#import "CustomTarBarViewController.h"
@implementation appdelegate
-(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) Launchoptions
{
self. Window = [[uiwindow alloc] initwithframe: [[UIScreen mainscreen] bounds]];
self. Window. backgroundcolor = [Uicolor whitecolor];
[self. Window makekeyandvisible];
This adds navigation to the first view or not (this shows the addition of navigation)
Uiviewcontroller *HOMEVC = [[Uiviewcontroller alloc] init] ;
Homevc.view. BackgroundColor = [Uicolor redcolor];
Uinavigationcontroller *nav = [[[Uinavigationcontroller alloc] Initwithrootviewcontroller : HOMEVC] autorelease];
Uiviewcontroller *SETVC = [[Uiviewcontroller alloc] init] ;
Setvc.view. BackgroundColor = [Uicolor bluecolor];
Uiviewcontroller *SHAREVC = [[Uiviewcontroller alloc] init] ;
Sharevc.view. BackgroundColor = [Uicolor yellowcolor];
Uiviewcontroller *plusvc = [[Uiviewcontroller alloc] init] ;
Plusvc.view. BackgroundColor = [Uicolor greencolor];
Uiviewcontroller *PLAYVC = [[Uiviewcontroller alloc] init];
Playvc.view. BackgroundColor = [Uicolor orangecolor];
Customtarbarviewcontroller *TABVC = [[Customtarbarviewcontroller alloc] init] ;
Add a view to TABVC
TABVC. viewcontrollers = [nsarray ARRAYWITHOBJECTS:NAV,SETVC,SHAREVC,PLUSVC,PLAYVC, Nil];
self. Window. Rootviewcontroller = TABVC;
return YES;
}
Custom Tabbar-ios Learning