iOS 自定义底部tabbar加号按钮实现方法
自定义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加号按钮实现方法的更多相关文章
- Wepy--小程序自定义底部tabBar
PS后续: 说来惭愧, 没想到这篇文章浏览的人有点多. 说实话写的挺乱的. 并且自定义tabbar还有闪屏的问题. 因为有好几位道友都问了这个问题, 其中一位因为项目很急,所以就研究了一下(也是借鉴 ...
- iOS开发项目之四 [ 调整自定义tabbar的位置与加号按钮的位置]
自定义tabbar与按钮的添加 01 - 把系统的tabbar用我们自己的覆盖 LHQTabBar *lhqTabBar = [[LHQTabBar alloc]init]; [self setVal ...
- iOS自定义tabBar
在我们的项目中经常会自己自定义tabBar因为苹果自带的真的太丑了!也不满足我们的项目需求. 好 开始行动吧! 先上图看下我们最终实现的效果: 继承UItabBar自定义一个自己的tabBar .h# ...
- iOS自定义的UISwitch按钮
UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...
- iOS 自定义返回按钮,保留系统滑动返回
原文链接 自定义返回按钮保留系统滑动返回手势.gif 1.简介 使用苹果手机,最喜欢的就是用它的滑动返回.作为一个开发者,我们在编写很多页面的时候,总是会因为这样那样的原因使得系统的滑动返回不可用.使 ...
- 拦截iOS系统导航栏返回按钮事件-三种方法
方法一:在dealloc里面书写监听事件,因为只有pop才会调用dealloc,push不会掉用 - (void)dealloc {YLLog(@"123"); } 方法二:在- ...
- iOS 自定义UINavigationController返回按钮
主要代码如下: //自定义导航栏返回按钮 self.navigationItem.leftBarButtonItem = ({ //导航栏返回背景视图 UIView *view = [[UIView ...
- iOS 自定义NavigationBar右侧按钮rightBarButtonItem
自定义右侧的一个按钮 UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"主页" style: ...
- iOS中UI阶段常用的一些方法
UI 即 UserInterface(用户界面 1.iOS系统版本,每年都有更新.对我们开发者而言,主要的是观察API的变化. 2.iPhone新手机发布,会产生不同尺寸的屏幕,现在市面上有4种尺寸, ...
随机推荐
- Windows10 VS2017 C++编译Linux程序
#include <cstdio> #include <iostream> #include "unistd.h" using namespace std; ...
- 神州数码DHCP及DHCP中继配置
实验要求:掌握DHCP及DHCP中继配置方法 拓扑如下 R1 enable 进入特权模式 config 进入全局模式 hostname R1 修改名称 interface g0/5 进入端口 ip a ...
- 3.9 run_main.py源码(兼容python2和3)
3.9 run_main.py源码(兼容python2和3) 以下代码在python2和python3上都跑通过,python3只需注释掉上面红色框框区域代码就行(最后一步发送邮箱代码,我注释掉了). ...
- Anaconda安装(Windows)
Anaconda集成了python和各种常用工具. 目前有两个版本,分别包含了python2.7和python3.5,并且自动添加环境变量.去官网下载安装包. 1.安装其他模块.conda insta ...
- Windows gitweb安装
/******************************************************************************* * Windows gitweb安装 ...
- 【Linux】scp指令
语法: scp [可选参数] file_source file_target 参数说明: -1: 强制scp命令使用协议ssh1 -2: 强制scp命令使用协议ssh2 -4: 强制scp命令只使用I ...
- flask中注册验证码和分页
注册验证码.核心思路,替换注册页面的img标签的src属性. 1.准备好文件夹:captcha2.导包 from utils.captcha.captcha import captcha3.验证码生成 ...
- 新 radio样式修改
https://blog.csdn.net/qq_41617704/article/details/80973966
- centos下源码编译安装MySQL
解压下载的软件压缩包 tar xzvf mysql-5.1.63.tar.gz 进入解压的目录 cd mysql-5.1.63/ 安装需要的依赖包 yum install gcc gcc-c++ ...
- Python 配置 selenium 模拟浏览器环境,带下载链接
使用浏览器渲染引擎.直接用浏览器在显示网页时解析HTML,应用CSS样式并执行JavaScript的语句. 这方法在爬虫过程中会打开一个浏览器,加载该网页,自动操作浏览器浏览各个网页,顺便把数据抓下来 ...