自定义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 ...
随机推荐
- Linux 的信号和线程
什么是线程 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成,每一个程序都至少 ...
- ueditor整合之后前段不显示高亮
自己整合ueditor插件后,发现在前段显示的时候并没有高亮处理 在网上看了一些方法后,自己尝试了下. 1.在页面上引入相关的js和css文件 主要是以下两个文件 <script src=&qu ...
- Chrome 浏览器安装Vue插件方法 (十分详细)
博主最近在研究Vue,无奈新手想安装Chrome的Vue插件,整理下安装流程: 1.首先去github下载vue.zip文件插件(还有npm安装方法这里就不介绍了自行百度)下载地址:https://g ...
- Centos7 下安装 RabbitMQ
安装 erlang 1.下载erlang 官网地址 http://www.erlang.org/download 挑选合适的版本 然后 wget 比如目前最新版本 19.3 运行命令 wget htt ...
- Spring实战Day4
profile与项目环境 使用profile根据环境创建bean,常用的情景是数据库的配置.一.配置方式1.在JavaConfig中配置 1.1在类上配置,生产环境不是"dev"的 ...
- mysql 建立utf8字符集数据库
CREATE DATABASE `evaluate` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
- [Tools] Convert SVG to a PDF in Node with PDFKit and SVG.js
Given a epxress application and an svg template, we want to draw some text, date onto it and convert ...
- Python源代码--整数对象(PyIntObject)的内存池
[背景] 原文链接:http://blog.csdn.net/ordeder/article/details/25343633 Python整数对象是不可变对象,什么意思呢?比如运行例如以下pytho ...
- Hadoop教程(一)
英文原文:cloudera,编译:ImportNew – Royce Wong Hadoop从这里开始!和我一起学习下使用Hadoop的基本知识,下文将以Hadoop Tutorial为主体带大家走一 ...
- 浅谈xss原理
近日,论坛上面XSS满天飞,各处都能够见到XSS的痕迹,前段时间论坛上面也出现了XSS的迹象.然后我等小菜不是太懂啊,怎么办?没办法仅仅有求助度娘跟谷歌这对情侣了. 能够说小菜也算懂了一些.不敢藏私, ...