创建继承与UITabBarViewController 的 ViewController
//// CustomViewController.m// EmpManager//// Created by _Tiny on 16/1/18.// Copyright © 2016年 dean. All rights reserved.//#import "CustomViewController.h"#import "MyViewController.h"@interface CustomViewController ()
@property(nonatomic,retain)UIButton *btn;@property(nonatomic,assign)BOOL isSel;@property(nonatomic,retain)NSMutableArray *allButtonArray;@end@implementation CustomViewController- (void)viewDidLoad { [super viewDidLoad]; self.tabBar.hidden = YES; [self tabBarCustom]; // Do any additional setup after loading the view.}-(void)tabBarCustom{ //首先移除当前的tabBar [self.tabBarController.tabBar removeFromSuperview]; //自定义view放按钮 UIView *myView = [[UIView alloc]init]; myView.frame = CGRectMake(0, 603*Height, 375*Width, Height*64); // self.tabBarController.delegate = self;*签协议,(使用selectedIndex属性)* [self.view addSubview:myView]; //设置按钮选中前图片 NSMutableArray *tabBarImage = [NSMutableArray arrayWithObjects:@"shouye1.png",@"tongxunli1.png",@"wode1.png", nil]; //设置按钮选中图片 NSMutableArray *tabBarSelImage = [NSMutableArray arrayWithObjects:@"shouyexuanzhong1.png",@"tongxunluxuanzhong1.png",@"wodexuanzhong", nil]; self.allButtonArray = [NSMutableArray array]; for (int i = 0; i < 3 ; i ++) { self.btn = [UIButton buttonWithType:UIButtonTypeCustom]; self.btn.frame = CGRectMake(Width*(125*i), 0, 125, 64);// self.btn.backgroundColor = [UIColor grayColor]; [self.allButtonArray addObject:self.btn]; [myView addSubview:self.btn]; self.btn.tag = i;//设置按钮的标记, 方便来索引当前的按钮,并跳转到相应的视图 [self.btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.btn setBackgroundImage:[UIImage imageNamed:[tabBarImage objectAtIndex:i]] forState:UIControlStateNormal]; [self.btn setBackgroundImage:[UIImage imageNamed:[tabBarSelImage objectAtIndex:i]] forState:UIControlStateSelected]; //初始设置为未选中 self.btn.selected = NO; //第一个按钮为选中状态 if (self.btn.tag == 0) { self.isSel = YES; }else{ self.isSel = NO; } if (self.isSel == YES) { self.btn.selected = YES; }else{ self.btn.selected = NO; } } }//按钮点击方法 -(void)clickBtn:(UIButton *)sender{ // NSMutableArray * arr = [NSMutableArray arrayWithArray:self.allButtonArray]; for (UIButton * bu in self.allButtonArray) { if (bu.tag == sender.tag) { bu.selected = YES; arr[sender.tag] = bu; }else{ bu.selected = NO; } } //最关键的一句,点击当前按钮,对应相应的tabBar self.selectedIndex = sender.tag; }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end 在Appdelegate.m#pragma mark - 加载自动登录的页面- (void)setupHomeViewController{ self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; HomeViewController *homeVC = [[HomeViewController alloc]init]; UINavigationController *navHomeVC = [[UINavigationController alloc]initWithRootViewController:homeVC]; AddressBookViewController *addressBVC = [[AddressBookViewController alloc]init]; UINavigationController *navVC = [[UINavigationController alloc]initWithRootViewController:addressBVC]; MyViewController *myVC = [[MyViewController alloc]init]; UINavigationController *navMyVC = [[UINavigationController alloc]initWithRootViewController:myVC]; CustomViewController *customVC = [[CustomViewController alloc]init]; customVC.viewControllers = [NSArray arrayWithObjects:navHomeVC,navVC,navMyVC, nil]; self.window.rootViewController = customVC; }
#pragma mark - 图片渲染- (UIImage *)renderImageWithImageName:(NSString *)imageName{ UIImage * image = [[UIImage imageNamed:imageName] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]; return image;}