Custom tabBarController for iOS
//
// TabBar. m
// TabBarDemo
//
// Created by LeeYunHeNB on 14-10-10.
// Copyright (c) 2014 XinMaHuTong. All rights reserved.
//
# Import "TabBar. h"
# Import "TabBarBase. h"
@ Interface TabBar ()
{
UIButton * btn;
}
@ Property (nonatomic, strong) UIButton * selectedBtn;
@ End
@ Implementation TabBar
-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil
{
Self = [superinitWithNibName: nibNameOrNil bundle: nibBundleOrNil];
If (self ){
// Custom initialization
}
Return self;
}
-(Void) viewDidLoad
{
[SuperviewDidLoad];
// Do any additional setup after loading the view.
}
-(Void) viewWillLayoutSubviews
{
Self. tabBarController. tabBar. hidden = NO; // hide the original tabBar
CGFloat tabBarViewY = self. view. frame. size. height-49;
UIView * _ tabBarView = [[UIViewalloc] initWithFrame: CGRectMake (0, tabBarViewY, 320, 49)];
// View settings are available here
[_ TabBarView setBackgroundColor: [UIColorgrayColor];
[Self. viewaddSubview: _ tabBarView];
For (int I = 0; I <2; I ++ ){
Btn = [[UIButtonalloc] init];
// Add an image to the button without adding an image
// NSString * imageName = [NSString stringWithFormat: @ "TabBar % d", I];
// NSString * imageNameSel = [NSString stringWithFormat: @ "TabBar % dSel", I];
// [Btn setImage: [UIImage imageNamed: imageName] forState: UIControlStateNormal];
// [Btn setImage: [UIImage imageNamed: imageNameSel] forState: UIControlStateSelected];
// [Btn setBackgroundColor: [UIColor greenColor];
If (I = 1 ){
[BtnsetTitle: @ "Straw hat 1" forState: UIControlStateNormal];
[BtnsetTitleColor: [UIColorredColor] forState: UIControlStateSelected];
}
If (I = 0 ){
[BtnsetTitle: @ "Straw hat 2" forState: UIControlStateNormal];
[BtnsetTitleColor: [UIColorredColor] forState: UIControlStateSelected];
}
CGFloat x = I * _ tabBarView. frame. size. width/2;
Btn. frame = CGRectMake (x, 0, _ tabBarView. frame. size. width/2, _ tabBarView. frame. size. height );
[_ TabBarView addSubview: btn];
// Set the first button to the selected status when entering
If (0 = I ){
Btn. selected = YES;
Self. selectedBtn = btn; // you can specify this button as the selected one.
}
Btn. tag = I; // set the button tag to easily index the current button and jump to the corresponding View
// Add button click event
[BtnaddTarget: selfaction: @ selector (clickBtn :) forControlEvents: UIControlEventTouchUpInside];
}
}
-(Void) clickBtn :( UIButton *) sender
{
If (sender. tag = 0 ){
Self. selectedIndex = 0;
}
If (sender. tag = 1 ){
Self. selectedIndex = 1;
}
// 1. Set the previously selected button to unselected
Self. selectedBtn. selected = NO;
// 2. Set the current button to selected.
Sender. selected = YES;
// 3. Assign the current button to the previously selected button
Self. selectedBtn = sender;
// 4. Jump to the corresponding View Controller. (use the selectIndex parameter to set the selected controller)
}
-(Void) didReceiveMemoryWarning
{
[SuperdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//-(Void) tabBar :( TabBarBase *) tabBar selectedFrom :( NSInteger) from to :( NSInteger) {
// Self. selectedIndex =;
//}
///*
# Pragma mark-Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
-(Void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
@ End
Demo download path http://download.csdn.net/detail/u013682799/8020471