用的时候直接拷贝代码即可.

1.在AppDelegate设置跟控制器为:PQTabBarController

#import "PQTabBarController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1.创建window
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
// 2.创建根控制器
PQTabBarController *tabBarVc = [[PQTabBarController alloc]init];
self.window.rootViewController = tabBarVc; // 3.显示window
[self.window makeKeyAndVisible];
return YES;
}

2.自定义TabBar,   PQTabBar.h文件:

#import <UIKit/UIKit.h>
@class PQTabBar; @protocol PQTabBarDelegate <NSObject>
- (void)tabBar:(PQTabBar *)tabBar didSelectedButtonFrom:(NSInteger)from to:(NSInteger)to; @end @interface PQTabBar : UIView
@property(nonatomic,weak)id<PQTabBarDelegate>delegate;
- (void)addTabBarButtonWith:(UITabBarItem *)item; @end

        PQTabBar.m文件:

#import "PQTabBar.h"
#import "PQTaBarButton.h"
@interface PQTabBar() @property (nonatomic,weak)UIButton * currentSelectedBtn; @end
@implementation PQTabBar - (void)addTabBarButtonWith:(UITabBarItem *)item{
// 创建按钮
PQTaBarButton *btn = [PQTaBarButton buttonWithType:UIButtonTypeCustom];
[self addSubview:btn];
// 设置图片
[btn setBackgroundImage:item.image forState:UIControlStateNormal];
[btn setBackgroundImage:item.selectedImage forState:UIControlStateSelected];
// 监听点击事件
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
// 设置默认选中的第0个按钮
NSInteger count = self.subviews.count;
if( == count){
[self btnClick:btn];
}
} - (void)layoutSubviews
{
NSInteger count = self.subviews.count;
CGFloat btnW = self.frame.size.width/;
CGFloat btnH = self.frame.size.height;
for (int i = ; i < count; i++) {
// 1.取出对应的按钮
UIButton *btn = self.subviews[i];
// 2.计算frame
CGFloat btnY = ;
CGFloat btnX = btnW * i;
// 3.设置frame
btn.frame = CGRectMake(btnX, btnY, btnW, btnH); // 4.设置按钮的tag
btn.tag = i;
}
} - (void)btnClick:(UIButton *)btn
{
//1.通知代理
if([self.delegate respondsToSelector:@selector(tabBar:didSelectedButtonFrom:to:)]){
[self.delegate tabBar:self didSelectedButtonFrom:self.currentSelectedBtn.tag to:btn.tag];
} // 取消上一次选中
self.currentSelectedBtn.selected = NO;
// 设置按钮为选中
btn.selected = YES;
// 记录当前选中的按钮
self.currentSelectedBtn = btn;
} @end

3.自定义TabBarItem.       PQTaBarButton.h文件:

#import <UIKit/UIKit.h>
@interface PQTaBarButton : UIButton
@end
                        PQTaBarButton.m文件中:
#import "PQTaBarButton.h"

@implementation PQTaBarButton
- (void)setHighlighted:(BOOL)highlighted{
}
@end

4.自定义UITabBarController,    PQTabBarController.h文件

#import <UIKit/UIKit.h>

@interface PQTabBarController : UITabBarController

@end

               PQTabBarController.m文件

#import "PQTabBarController.h"
#import "PQGameViewController.h"
#import "PQMenuViewController.h"
#import "PQMessageViewController.h"
#import "PQCommunityViewController.h"
#import "PQReservationViewController.h"
#import "PQTabBar.h"
#import "PQTaBarButton.h" @interface PQTabBarController ()<PQTabBarDelegate> @property (nonatomic,weak)PQTabBar * customTabBar;
@end @implementation PQTabBarController - (void)viewDidLoad {
[super viewDidLoad];
// 2.添加自定义的tabBar
[self setupTabBar];
//添加子控制器
[self setUpAllChlidVc]; } - (void)setupTabBar
{
// 1.添加自定义
PQTabBar *cutomTabBar = [[PQTabBar alloc]init];
cutomTabBar.backgroundColor = [UIColor greenColor];
cutomTabBar.frame = self.tabBar.frame;
self.customTabBar = cutomTabBar; // 成为自定义tabBar的代理
cutomTabBar.delegate = self;
[self.view addSubview:cutomTabBar]; // 2.删除系统自带
[self.tabBar removeFromSuperview]; }
- (void)setUpAllChlidVc
{
PQGameViewController *gameVc = (PQGameViewController *)[self addChildVcWithName:@"PQGameViewController" title:@"游戏" image:@"TabBar1" selectedImage:@"avater"]; PQMenuViewController *menuVc = (PQMenuViewController *)[self addChildVcWithName:@"PQMenuViewController" title:@"菜单" image:@"TabBar2" selectedImage:@"avater"]; PQMessageViewController *messageVc = (PQMessageViewController *)[self addChildVcWithName:@"PQMessageViewController" title:@"消息" image:@"TabBar3" selectedImage:@"avater"]; PQCommunityViewController *communityVc = (PQCommunityViewController *)[self addChildVcWithName:@"PQCommunityViewController" title:@"社区" image:@"TabBar4" selectedImage:@"avater"]; PQReservationViewController *reservationVc = (PQReservationViewController *)[self addChildVcWithName:@"PQReservationViewController" title:@"待选" image:@"TabBar5" selectedImage:@"avater"]; self.viewControllers = @[gameVc , menuVc , messageVc , communityVc , reservationVc]; } - (UIViewController *)addChildVcWithName:(NSString *)vcName title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:vcName bundle:nil];
UIViewController *Vc = sb.instantiateInitialViewController; //设置对应控制器按钮的图片
Vc.tabBarItem.image = [UIImage imageNamed:image];
Vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];// 调用自定义TabBar创建按钮的方法
[self.customTabBar addTabBarButtonWith:Vc.tabBarItem];
return Vc;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} #pragma mark 代理方法
- (void)tabBar:(PQTabBar *)tabBar didSelectedButtonFrom:(NSInteger)from to:(NSInteger)to{ self.selectedIndex = to;
} @end

自定义UITabBarController的更多相关文章

  1. 自定义UITabbarController控制器

    自定义UITabbarController控制器 这是定制UITabbarController的基本原理,没有进行功能性封装. 效果:   源码地址: https://github.com/YouXi ...

  2. iOS-自定义 UITabBarController

    先来回顾一下UITabBarController ( 稍微详细的在在http://blog.csdn.net/yang198907/article/details/49807011) 伴随UITabB ...

  3. 自定义UITabBarController标签视图控制器

    首先创建一个类,继承自UItabBarController 然后在.m文件中: 这里我有两个宏定义: #define WIDTH (myView.frame.size.width / 4) //我在写 ...

  4. IOS开发之自定义UITabBarController

    UITabBarController是开发中经常会用到的一个视图控制器,但是默认的UITabBarController经常不能够完全满足我们的需求,所以我们经常需要自定义一个UITabBarContr ...

  5. iOS 自定义UITabBarController的tabBar

               #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDeleg ...

  6. xcode6 自定义UITabbarController

    -(void)initTabBarView{ if (tabBarController && [kAPPDELEGATE.navigationController.viewContro ...

  7. UITabBarController底层实现

    1.首先要了解:任何控制器,都能添加子控制器      UIViewController里面有一个方法:     - (void)addChildViewController:(UIViewContr ...

  8. iOS去除导航栏和tabbar的1px横线

    1.在自己定义的导航栏中或者设计稿中经常需要去除导航栏的1px横线,主要是颜色太不协调了 去除之前的图片 要去除这1px的横线,首先应该知道它是什么,在Xcode的界面调试中可以看到,它其实是UIIm ...

  9. Unbalanced calls to begin/end appearance transitions for **

    在自定义UITabBarController中点击视图跳转的时候,有可能就出现这个问题, 解决方法就是在自定义的UITabBarController中的视图显示消失通知方法中添加如下方法: - (vo ...

随机推荐

  1. iOS7 毛玻璃效果

    转自:http://prolove10.blog.163.com/blog/static/138411843201391401054305/ 原图:  效果图:  实现:首先需要导入Accelerat ...

  2. 搭建Redis环境以及所遇问题(CentOS7+Redis 3.2.8)

    一.安装步骤 1. 首先需要安装gcc,把下载好的redis-3.2.8-rc2.tar.gz 放到/usr/local文件夹下 2. 进行解压 tar -zxvf redis-3.2.8-rc2.t ...

  3. 洛谷——P2690 接苹果

    P2690 接苹果 题目背景 USACO 题目描述 很少有人知道奶牛爱吃苹果.农夫约翰的农场上有两棵苹果树(编号为1和2), 每一棵树上都长满了苹果.奶牛贝茜无法摘下树上的苹果,所以她只能等待苹果 从 ...

  4. 树莓派LED指示灯说明

    原文:http://shumeipai.nxez.com/2014/09/30/raspberry-pi-led-status-detail.html?variant=zh-cn LED亮灯状态 LE ...

  5. linux svn配置hooks

    先创建仓库: svnadmin create /data/svn/my.com 再配置权限: #cd /data/svn/my.com/conf/ #vim svnserve.conf 配置 [gen ...

  6. Linux线上系统程序debug思路及方法

    http://blog.csdn.net/wangzuxi/article/details/44766221

  7. excel转换html

    利用POI解析excel,转换成html,支持各种版本的excel.支持自定义样式.支持行列合并 需要用到的jar public class Excel2Html { /** * 读取Excel并转换 ...

  8. 公司hadoop客户端试用

    今天用了一下公司的hadoop客户端,从外面下载的客户端不能用,只能用这个wiki里面提供的:link 装在了 tc-cm-201511novam12x12n0.tc 目录 /home/work/vi ...

  9. Ikki&#39;s Story IV - Panda&#39;s Trick (poj 3207 2-SAT)

    Language: Default Ikki's Story IV - Panda's Trick Time Limit: 1000MS   Memory Limit: 131072K Total S ...

  10. Mesa (computer graphics)

    http://en.wikipedia.org/wiki/Mesa_(computer_graphics) Mesa (computer graphics) From Wikipedia, the f ...