自定义UITabBarController

摘要:
使用时直接复制代码。1.将AppDelegate中的以下控制器设置为:PQTabBarController#import“PQTabBarController.h”@interfaceAppDelegate@end@implementationAppDelegate-(BOOL)应用程序:(UIApplication*)applicationdidFinishLaunchingW

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

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

#import "PQTabBarController.h"
@interfaceAppDelegate ()
@end
@implementationAppDelegate
- (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];
    returnYES;
}

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

#import <UIKit/UIKit.h>
@classPQTabBar;

@protocol PQTabBarDelegate <NSObject>
- (void)tabBar:(PQTabBar *)tabBar didSelectedButtonFrom:(NSInteger)from to:(NSInteger)to;

@end

@interfacePQTabBar : UIView
@property(nonatomic,weak)id<PQTabBarDelegate>delegate;
- (void)addTabBarButtonWith:(UITabBarItem *)item;

@end

PQTabBar.m文件:

#import "PQTabBar.h"
#import "PQTaBarButton.h"
@interfacePQTabBar()

@property (nonatomic,weak)UIButton *currentSelectedBtn;

@end
@implementationPQTabBar



- (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(1 ==count){
        [self btnClick:btn];
    }
}

- (void)layoutSubviews
{
    NSInteger count =self.subviews.count;
    CGFloat btnW = self.frame.size.width/5;
    CGFloat btnH =self.frame.size.height;
    for (int i = 0; i < count; i++) {
        //1.取出对应的按钮
        UIButton *btn =self.subviews[i];
        //2.计算frame
        CGFloat btnY = 0;
        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.delegaterespondsToSelector:@selector(tabBar:didSelectedButtonFrom:to:)]){
        [self.delegatetabBar: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>
@interfacePQTaBarButton : UIButton
@end
                        PQTaBarButton.m文件中:
#import "PQTaBarButton.h"

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

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

#import <UIKit/UIKit.h>

@interfacePQTabBarController : 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

@implementationPQTabBarController

- (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];
    returnVc;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];    
}


#pragma mark 代理方法
- (void)tabBar:(PQTabBar *)tabBar didSelectedButtonFrom:(NSInteger)from to:(NSInteger)to{
    
    self.selectedIndex =to;
}

@end

免责声明:文章转载自《自定义UITabBarController》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇rsync的相关使用,参数设置。[二] JavaIO之File详解 以及FileSystem WinNTFileSystem简介下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

ThinkPHP

ThinkPHP的开发模式是define//Debug mode define//当运行模式控制器的操作系统找不到请求的方法时,它将定位__Empty()方法处理。使用此机制,我们可以统一处理用户请求的所有不存在的操作。模块分组大A函数和大R函数有什么区别?关联数组易于操作,信息量相对较大...

Spring通过MimeMessageHelper发送邮件,中文附件名出现乱码解决办法

1.设置系统值system。setProperty(“mail.mime.split-longparameters”,“false”);2.在这里,定义创建对象时的编码格式(utf-8):MimeMessageHelper=newMimeMessageHelper(mes,true,“utf-8”);3.其次,添加附件时,附件名称为helper。需要定义代码...

IntelliJ IDEA(2017)安装和破解

IDEA全称IntelliJIDEA,是Java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手、代码自动提示、重构、J2EE支持、各类版本工具、JUnit、CVS整合、代码分析、创新的GUI设计等方面的功能可以说是超常的。用记事本打开分别在最下面一行增加一行:-javaagent:D:IDEAinJetbra...

axios 处理超时问题 记录

前言:记录最近两天处理请求超时的逻辑。...

backgroundsize

当背景大小值为和时,可以设置两个值,也可以设置一个值。当只取一个值时,第二个值相当于auto,但此处的auto不会将背景图像的高度保持在其原始高度,而是与第一个值相同。此外,如果只取一个值,宽度和高度将相同,这相当于背景大小:80%自动。...

数据库软考易混淆知识之信息化基础、项目管理

2、 关键路径关键路径是活动图中最长的路径示例:图中显示了软件项目的活动图,其中固定点表示项目里程碑,连接顶点的边表示包含的活动,边上的数字表示活动持续时间的天数,则完成项目的最短时间为()天,活动EH和IJ的放松时间分别为()日。...