自定义TabBar

来源:互联网 发布:ubuntu文件服务器搭建 编辑:程序博客网 时间:2024/04/27 10:04

.h文件

////  MyTabBar.h////#import <UIKit/UIKit.h>/* 1.代理:1.2.3.4.5.6. 2.自定义控件时: initWithFrame和layoutSubviews两个方法的作用 */@class MyTabBar;//因为MyTabBar继承自UITabBar,所以称为MyTabBar的代理,也必须实现UITabBar的代理协议@protocol MyTabBarDelegate <UITabBarDelegate>@optional- (void)tabBarDidClickPlusButton:(MyTabBar *)tabBar;@end@interface MyTabBar : UITabBar@property (nonatomic, weak) id<MyTabBarDelegate> delegate;@end

.m文件

////  MyTabBar.m////#import "MyTabBar.h"@interface MyTabBar()@property (nonatomic, weak) UIButton *plusBtn;@end@implementation MyTabBar// 控件初始化=================/* 1.为什么要在initWithFrame:方法而不是在init方法?    因为使用纯代码的方式创建自定义类,在以后使用的时候可能使用init方法创建,也有可能使用initWithFrame:方法创建,但是无论哪种方式,最后都会调用到initWithFrame:方法。在这个方法中创建子控件,可以保证无论哪种方式都可以成功创建。 2.为什么要在initWithFrame:方法里面只是将子控件加到view而不设置尺寸    (1)前面已经说过,两种方式最后都会调用到initWithFrame:方法。如果使用init方法创建,那么这个view的frame有可能是不确定的:    (2)如果是这种情况,那么在init方法中,frame是不确定的,此时如果在initWithFrame:方法中设置尺寸,那么各个子控件的尺寸都会是0,因为这个view的frame还没有设置。(可以看到是在发送完init消息才设置的) 3. 想要在layoutSubviews访问到的话,一般需要创建这个子控件的对应属性来指向它。    self.plusBtn = plusBtn; 4.注意对应属性这里,限制词使用weak就可以,因为button已经被加入到self.view.subviews这个数组里。 */- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // 添加一个按钮到tabbar中        UIButton *plusBtn = [[UIButton alloc] init];        // backgroundImage        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];        // " + "号按钮        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];        plusBtn.size = plusBtn.currentBackgroundImage.size;        // 添加响应事件。        [plusBtn addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];        [self addSubview:plusBtn];        self.plusBtn = plusBtn;    }    return self;}/** *  加号按钮点击 */- (void)plusClick{    // 通知代理    if ([self.delegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {        [self.delegate tabBarDidClickPlusButton:self];    }}// 重新布局=================- (void)layoutSubviews{    // 一定要先使用这个方法!!!    [super layoutSubviews];    // 1.设置加号按钮的位置    self.plusBtn.centerX = self.width * 0.5;    self.plusBtn.centerY = self.height * 0.5;    // 2.设置其他tabbarButton的位置和尺寸    CGFloat tabbarButtonW = self.width / 5;    CGFloat tabbarButtonIndex = 0;    for (UIView *child in self.subviews) {        Class class = NSClassFromString(@"UITabBarButton");        if ([child isKindOfClass:class]) {            // 设置宽度            child.width = tabbarButtonW;            // 设置x            child.x = tabbarButtonIndex * tabbarButtonW;            // 增加索引            tabbarButtonIndex++;            // 跳过中间“+”号按钮.            if (tabbarButtonIndex == 2) {                tabbarButtonIndex++;            }        }    }}@end

在tabBarController中使用自定义的tabBar

////  MyTabBarViewController.m//  NetWork////  Created by lcy on 17/3/13.//  Copyright © 2017年 NJ. All rights reserved.//#import "MyTabBarViewController.h"#import "fistViewController.h"#import "secondViewController.h"#import "thirdViewController.h"#import "forthViewController.h"#import "MyTabBar.h"@interface MyTabBarViewController ()<MYTabBarDelegate>@end@implementation MyTabBarViewController- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    [self addChildVc:[[fistViewController alloc] init] title:@"首页" image:@"1" selectedImage:@"1"];    [self addChildVc:[[secondViewController alloc] init] title:@"社区" image:@"2" selectedImage:@"2"];    [self addChildVc:[[thirdViewController alloc] init] title:@"消息" image:@"3" selectedImage:@"3"];    [self addChildVc:[[forthViewController alloc] init] title:@"咨询" image:@"4" selectedImage:@"4"];    MyTabBar *tabBar = [[MyTabBar alloc] init];    //取消tabBar的透明效果    tabBar.translucent = NO;    tabBar.myTabBarDelegate = self;    // KVC:如果要修系统的某些属性,但被设为readOnly,就是用KVC,即setValue:forKey:。    // 错误写法:self.tabBar = tabBar;    [self setValue:tabBar forKey:@"tabBar"];}- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{    childVc.title = title;    childVc.tabBarItem.image = [UIImage imageNamed:image];    // 禁用图片渲染    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];    // 设置文字的样式    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateNormal];    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor yellowColor]} forState:UIControlStateSelected];    UINavigationController *navigationVc = [[UINavigationController alloc] initWithRootViewController:childVc];    [self addChildViewController:navigationVc];}// 代理方法实现- (void)tabBarDidClickPlusButton:(MyTabBar *)tabBar{    NSLog(@"点击了++号");}@end
0 0
原创粉丝点击