自定义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 ...
随机推荐
- R必学包之dplyr
http://www.360doc.com/content/17/1204/10/50223086_709726679.shtml
- Android-一张图理解MVP的用法
M和V通过P交互,M做了两件事,开启子线程做耗时操作,然后使用原生的Hander方式切回主线程回调结果给P. M做的两件事也可以使用比较流行的rxjava实现: 备注:图片不清晰可以看这里
- Maven生成项目文档
Maven项目可以通过maven-site-plugin插件生成项目文档,无论什么项目都可以生成. 执行命令: mvn site 生成完成的输出目录在${basedir}/target/site文件夹 ...
- cocos2d-x 3.0 final 移植 android
准备工作 你仅仅要依照上一篇的 cocos2d-x 3.0 final 环境搭建 完毕就能够了 1.编辑proj.android\jni\Android.mk,更改内容例如以下 LOCAL_PATH ...
- 微信小程序 - 提取字体图标与其优化
微信小程序,无论是字体图标还是图标,都差不多,只不过是为了以后字体图标修改方便,或者加效果方便而使用它而已! 1. 下载font-awesome http://fontawesome.dashgame ...
- window.onresize 事件笔记
1.浏览器尺寸变化响应事件 : window.onresize = function(){....} 这里须要注意的是,onresize响应事件处理中.获取到的页面尺寸參数是变更后的參数. // ...
- 如何给老婆解释什么是RESTful
如何给老婆解释什么是RESTful Javdroider Hong 知乎专栏<Beautiful Java>的作者,一个热爱足球和健身的上进boy 1,543 人赞了该文章 老婆经常喜欢翻 ...
- 猫猫学IOS(二)UI之button操作 点击变换 移动 放大缩小 旋转
不多说,先上图片看效果,猫猫分享.必须精品 原创文章.欢迎转载.转载请注明:翟乃玉的博客 地址:viewmode=contents">http://blog.csdn.net/u013 ...
- HDU 2955 Robberies(01背包变形)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- wxpython中控件对键盘输入无响应的可能原因
问题描述: 开发环境:Win7 32bit + Python2.7.6 + WxPython 3.0.1-b20140707 开发某初级CAD软件中,需要实现点击TreeCtrl控件的相应选择,实现G ...