自定义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 ...
随机推荐
- LCA rmq st model
LCA:倍增 memset(p,-,sizeof(p)); inline void dfs(int u) { ;i=e[i].next) { int v=e[i].v; ) { deep[v]=dee ...
- Java主线程等待所有子线程执行完毕再执行解决办法(转)
方法一: Thread.join()方法,亲测可行,thread.join()方法 Vector<Thread> ts = new Vector<Thread>(); for ...
- IOS -- base64编码
在iOS7以后可以用NSData自带的base64EncodedStringWithOptions进行编解码: 方法如下: - (NSString *)encodeToBase64String:(UI ...
- 关于linter
各类代码都有规则格式检查工具,称之为linter 比如:csslint/jslint/eslint/pylint sumlime提供了一个linter的框架SublimeLinter,在里面可以使用各 ...
- 公司hadoop客户端试用
今天用了一下公司的hadoop客户端,从外面下载的客户端不能用,只能用这个wiki里面提供的:link 装在了 tc-cm-201511novam12x12n0.tc 目录 /home/work/vi ...
- C# UserControl 判断是否是设计模式中
In Windows Forms application, we can use Control.IsInDesignMode or LicenseManager.UsageMode == Licen ...
- BUPT复试专题—图像压缩存储(2015)
题目描述 以二维数组表示图像,其值只有0.1两种,寻找两幅图像中最大的相同方阵 输入 第一行输入一个n,接下来的2n行输入两个n*n数组,寻找一个最大的m*m子区域,使得两个数组在该子区域完全相同 ...
- SolidEdge 装配体中如何快速的搞定一个面上所有螺丝 如何在装配体上进行阵列
1 点击"规则排列" 选择要排列的螺丝 2 选择被规则排列的架子 3 选择所有的圆孔(鼠标滑到任意圆孔位置,左键单击即可选中所有圆孔) 4 选择参考的基准孔(已经上了螺 ...
- 哈理工2015 暑假训练赛 zoj 2976 Light Bulbs
MS Memory Limit:65536KB 64bit IO Format:%lld & %llu SubmitStatusid=14946">Practice ...
- HDU2084_数塔【简单题】【数塔】
数塔 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...