AppDelegate:
#import "AppDelegate.h"
#import "BaseTabBarController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
BaseTabBarController *uiTabBarController =[[BaseTabBarController alloc]initWithNibName:nil bundle:nil];
OneViewController *oneC=[[OneViewController alloc] initWithNibName:@"OneViewController" bundle:nil];
TwoViewController *twoC=[[TwoViewController alloc] initWithNibName:@"TwoViewController" bundle:nil];
ThreeViewController *threeC=[[ThreeViewController alloc] initWithNibName:@"ThreeViewController" bundle:nil];
NSArray *arrVC=[NSArray arrayWithObjects:oneC,twoC,threeC, nil];
[uiTabBarController setViewControllers:arrVC];
[uiTabBarController showItems];
self.window.rootViewController=uiTabBarController;
return YES;
}
BaseTabBarController.h:
#import <UIKit/UIKit.h>
@interface BaseTabBarController : UITabBarController
- (void)onTabButtonClik:(id)sender;
- (void)showItems;
BaseTabBarController.m:
//
// BaseTabBarController.m
// tabbar
//
// Created by dong on 14-5-20.
// Copyright (c) 2014年 dong. All rights reserved.
//
#import "BaseTabBarController.h"
@interface BaseTabBarController ()
@end
@implementation BaseTabBarController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)showItems{
for (int i=0; i<3; i++) {
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(i*109, 0, 109, 50)];
[btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"normal%d.png",i+1]] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"press%d.png",i+1]] forState:UIControlStateSelected];
// [btn setBackgroundImage:[UIImage imageNamed:@"mineCirclePress.png"] forState:UIControlStateHighlighted];
[btn setTag:i+1];
[btn addTarget:self action:@selector(onTabButtonClik:) forControlEvents:UIControlEventTouchUpInside];
[self.tabBar addSubview:btn];
if (i==0) {
[self onTabButtonClik:btn];
}
}
[self.tabBar setClipsToBounds:YES];
}
-(void)onTabButtonClik:(id)sender{
UIButton *btn=(UIButton*)sender;
//未被选中的按钮复位
for (UIView *v in self.tabBar.subviews) {
if ([v isKindOfClass:[UIButton class]]&&v.tag>0&&v.tag<4&&v.tag!=btn.tag) {
UIButton *btnv=(UIButton *)v;
[btnv setSelected:NO];
}
}
//select the selected tab
[btn setSelected:YES];
[self setSelectedIndex:btn.tag-1];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#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