自定义UITabBar替换系统默认的,目的是为了在UITabBar中间位置添加一个“+号按钮”

1、自定义WBTabBar,让其继承自UITabBar,并定义点击事件代理方法。

.h方法里面

#import <UIKit/UIKit.h>

@class WXReleaseTabBar;

@protocol WXReleaseTabBarDelegate <WXReleaseTabBarDelegate>

@optional

- (void)tabBarDidClickPlusButton:(WXReleaseTabBar *)tabBar;

@end

@interface WXReleaseTabBar : UITabBar

@property (nonatomic, weak) id<WXReleaseTabBarDelegate> tabBarDelegate;

@end

.m方法里面

//定义一个按钮

@property (nonatomic, strong) UIButton *plusButton;

然后初始化

- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

UIButton *plusBtn = [[UIButton alloc] init];

[plusBtn setBackgroundImage:[UIImage imageNamed:@"shareicon"] forState:UIControlStateNormal];

[plusBtn setBackgroundImage:[UIImage imageNamed:@"shareicon"] forState:UIControlStateHighlighted];

[plusBtn setImage:[UIImage imageNamed:@"shareicon"] forState:UIControlStateNormal];

[plusBtn setImage:[UIImage imageNamed:@"shareicon"] forState:UIControlStateHighlighted];

plusBtn.size = plusBtn.currentBackgroundImage.size;

[plusBtn addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:plusBtn];

self.plusButton = plusBtn;

}

return self;

}

//添加约束

- (void)layoutSubviews

{

[super layoutSubviews];

CGFloat width = self.width;

CGFloat height = self.height;

self.plusButton.center = CGPointMake(width * 0.5, height * 0.5);

CGRect tempRect = self.plusButton.frame;

tempRect.origin.y =  -20;

self.plusButton.frame = tempRect;

int index = 0;

CGFloat tabBarButtonW = width / 5;

CGFloat tabBarButtonH = height;

CGFloat tabBarButtonY = 0;

for (UIView *tabBarButton in self.subviews) {

if (![NSStringFromClass(tabBarButton.class) isEqualToString:@"UITabBarButton"]) continue;

CGFloat tabBarButtonX = index * tabBarButtonW;

if (index >= 2) {

tabBarButtonX += tabBarButtonW;

}

tabBarButton.frame = CGRectMake(tabBarButtonX, tabBarButtonY, tabBarButtonW, tabBarButtonH);

index++;

}

[self bringSubviewToFront:self.plusButton];

}

//重写系统的hitTest方法让处于tabbar外部的按钮部分也可以被点击

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

if (self.isHidden == NO) { // 当前界面 tabBar显示

CGPoint newPoint = [self convertPoint:point toView:self.plusButton];

if ( [self.plusButton pointInside:newPoint withEvent:event]) { // 点 属于按钮范围

return self.plusButton;

}else{

return [super hitTest:point withEvent:event];

}

}else {

return [super hitTest:point withEvent:event];

}

}

//被点击的方法

- (void)plusClick

{

// 通知代理

if ([self.tabBarDelegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {

[self.tabBarDelegate tabBarDidClickPlusButton:self];

}

}

2、tabBar是UITabBarController的只读成员变量(属性),是不让修改的,在UITabBarController.m中的操作如下:

@interface WXTabbarViewController ()<UITabBarDelegate>

@end

3、然后在UITabBarController.m初始化里面我们可以使用KVC的方式,更换系统自带的UITabBar,实现代码如下:

xxxTabBar(自定义的tabbar) *tabBar = [[xxxTabBar alloc] init]; 

xxxTabBar.tabBarDelegate= self;
[self setValue:tabBar forKeyPath:@"tabBar"];

4、在UITabBarController.m实现点击的代理方法

- (void)tabBarDidClickPlusButton:(WXReleaseTabBar *)tabBar

{

NSLog(@"发布按钮");

//    ComposeViewController *composeViewController= [[ComposeViewController alloc] init];

//    UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:composeViewController];

//    [self presentViewController:navigationController animated:YES completion:nil];

}

iOS 自定义底部tabbar加号按钮实现方法的更多相关文章

  1. Wepy--小程序自定义底部tabBar

    PS后续: 说来惭愧, 没想到这篇文章浏览的人有点多. 说实话写的挺乱的. 并且自定义tabbar还有闪屏的问题. 因为有好几位道友都问了这个问题,  其中一位因为项目很急,所以就研究了一下(也是借鉴 ...

  2. iOS开发项目之四 [ 调整自定义tabbar的位置与加号按钮的位置]

    自定义tabbar与按钮的添加 01 - 把系统的tabbar用我们自己的覆盖 LHQTabBar *lhqTabBar = [[LHQTabBar alloc]init]; [self setVal ...

  3. iOS自定义tabBar

    在我们的项目中经常会自己自定义tabBar因为苹果自带的真的太丑了!也不满足我们的项目需求. 好 开始行动吧! 先上图看下我们最终实现的效果: 继承UItabBar自定义一个自己的tabBar .h# ...

  4. iOS自定义的UISwitch按钮

    UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...

  5. iOS 自定义返回按钮,保留系统滑动返回

    原文链接 自定义返回按钮保留系统滑动返回手势.gif 1.简介 使用苹果手机,最喜欢的就是用它的滑动返回.作为一个开发者,我们在编写很多页面的时候,总是会因为这样那样的原因使得系统的滑动返回不可用.使 ...

  6. 拦截iOS系统导航栏返回按钮事件-三种方法

    方法一:在dealloc里面书写监听事件,因为只有pop才会调用dealloc,push不会掉用 - (void)dealloc {YLLog(@"123"); } 方法二:在- ...

  7. iOS 自定义UINavigationController返回按钮

    主要代码如下: //自定义导航栏返回按钮 self.navigationItem.leftBarButtonItem = ({ //导航栏返回背景视图 UIView *view = [[UIView ...

  8. iOS 自定义NavigationBar右侧按钮rightBarButtonItem

    自定义右侧的一个按钮 UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"主页" style: ...

  9. iOS中UI阶段常用的一些方法

    UI 即 UserInterface(用户界面 1.iOS系统版本,每年都有更新.对我们开发者而言,主要的是观察API的变化. 2.iPhone新手机发布,会产生不同尺寸的屏幕,现在市面上有4种尺寸, ...

随机推荐

  1. 页面添加锚点后如何点击不改变URL?

    直接奔主题,前端简单地锚点实现方法大家都会,无非就是在把 a 标签的 href 写成想要跳到的元素的id ,比如点击 <a href="#box"></a> ...

  2. 关于mybatis配置文件mapper传int值的问题

    1.首先看mapper代码,这是个更新语句. <set> <if test="sendmode!='' && sendmode!=null"> ...

  3. Eureka的初识

    在bili看完spring cloud eureka高可用注册中心的视频以后总结: 正常开发中,肯定有一个功能聚集服务中心,将功能方法全部写入其中,也就是一个springboot项目.该服务配置如下: ...

  4. 阿里云挂载硬盘(windows)

    1.运行:diskmgmt.msc,打开磁盘管理,看到未分配的盘符.点右键,新建简单卷: 2.设置卷大小.根据实际情况可设多设置. 3.分配盘符.格式化: 4.格式化完成: 5.看到了新分区:

  5. ionic3/4 使用NavController 返回两层的方式

    ionic3/4 使用NavController 返回两层的方式:  this.navCtrl.popTo(this.navCtrl.length() - 3);

  6. jQuery-3.事件篇---键盘事件

    jQuery键盘事件之keydown()与keyup()事件 鼠标有mousedown,mouseup之类的事件,这是根据人的手势动作分解的2个触发行为.相对应的键盘也有这类事件,将用户行为分解成2个 ...

  7. 【EMV L2】GPO响应以及AIP、AFL

    [GPO命令] 终端通过GPO(Get Processing Options)命令 通知卡片交易开始.命令数据为PDOL指定的终端数据. [GPO响应] 卡片在GPO命令的响应中返回AIP和AFL:A ...

  8. linux nginx 如何配置多个端口

    在linux下发布.netcore 应用,并使用nginx进行反向代理,参照博客园文章 https://www.cnblogs.com/ants/p/5732337.html#autoid-7-3-0 ...

  9. word中插入myth type公式行距变大的问题

    在写文章时,我遇到了在word中插入myth type公式时,行距明显变大的问题,我通过改变段落中的行距没有解决问题,在网上查了一下,找到一些解决方法,仅供参考. 解决办法

  10. xdoj-1297 Tr0y And His Startup

    题目: 1297: Tr0y And His Startup 时间限制: 1 Sec  内存限制: 256 MB提交: 18  解决: 8[提交][状态][讨论版] 题目描述 Tr0y创办了一家安全公 ...