自定义UITabBarController
用的时候直接拷贝代码即可.
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的更多相关文章
- 自定义UITabbarController控制器
自定义UITabbarController控制器 这是定制UITabbarController的基本原理,没有进行功能性封装. 效果: 源码地址: https://github.com/YouXi ...
- iOS-自定义 UITabBarController
先来回顾一下UITabBarController ( 稍微详细的在在http://blog.csdn.net/yang198907/article/details/49807011) 伴随UITabB ...
- 自定义UITabBarController标签视图控制器
首先创建一个类,继承自UItabBarController 然后在.m文件中: 这里我有两个宏定义: #define WIDTH (myView.frame.size.width / 4) //我在写 ...
- IOS开发之自定义UITabBarController
UITabBarController是开发中经常会用到的一个视图控制器,但是默认的UITabBarController经常不能够完全满足我们的需求,所以我们经常需要自定义一个UITabBarContr ...
- iOS 自定义UITabBarController的tabBar
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDeleg ...
- xcode6 自定义UITabbarController
-(void)initTabBarView{ if (tabBarController && [kAPPDELEGATE.navigationController.viewContro ...
- UITabBarController底层实现
1.首先要了解:任何控制器,都能添加子控制器 UIViewController里面有一个方法: - (void)addChildViewController:(UIViewContr ...
- iOS去除导航栏和tabbar的1px横线
1.在自己定义的导航栏中或者设计稿中经常需要去除导航栏的1px横线,主要是颜色太不协调了 去除之前的图片 要去除这1px的横线,首先应该知道它是什么,在Xcode的界面调试中可以看到,它其实是UIIm ...
- Unbalanced calls to begin/end appearance transitions for **
在自定义UITabBarController中点击视图跳转的时候,有可能就出现这个问题, 解决方法就是在自定义的UITabBarController中的视图显示消失通知方法中添加如下方法: - (vo ...
随机推荐
- 基于CI框架的管理系统
1:ci框架是有入口文件的,前端和后台入口文件(index.php,admin.php):里面修改$application_folder = 'application/home': 2:项目基本都是在 ...
- SGU104 二维dp
大致题意: n个东西放在(1.2.3...m)个容器中,先放的必需在后方的左边.a[i][j]表示i号物品放在j容器所得 的价值,求最大价值. 几乎是刚刚开始接触动态规划题,开始我这样想 每个东西一件 ...
- android test控件
1.Plain Text 输入文本框 <EditText android:id="@+id/editText" android:layout_width="wrap ...
- 前端进阶之路:初涉Less
阅读目录 一.Less介绍 1.官方介绍 2.自己理解 3.Less.Sass.Stylus 二.Less使用入门 1.开发模式下使用Less 2.运行模式下使用Less 三.常见用法示例 1.从第一 ...
- Java 读写文件大全
原文:http://www.open-open.com/code/view/1423281836529 java中多种方式读文件 一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内 ...
- 论DATASNAP结合FIREDAC的使用方法
论DATASNAP结合FIREDAC的使用方法 自DELPHI XE5开始引入FIREDAC数据引擎以来,FIREDAC就正式成为了官方的数据引擎.一直到XE10.1 UPDATE1,据笔者观察,FI ...
- springboot mybatis 项目框架源码 shiro 集成代码生成器 ehcache缓存
1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器)freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.service等完整模块2. ...
- Java中的反射机制,利用反射访问私有
利用反射,首先是Class对象的获取,之后是Method和Field对象的获取. 以Method为例,从文档中可以看到: getMethod()方法返回的是public的Method对象, 而getD ...
- 转:NetBeans的远程Linux C开发实践
转: http://blog.csdn.net/jacktan/article/details/9268535 一直以来总觉得NetBeans生活在Eclipse的阴影下,同样做为一款不错的基于Jav ...
- UltraEdit UE如何取消保存文件自动备份
高级-配置-文件处理-备份,设置为不备份