iOS之自定义UITabBar替换系统默认的(添加“+”号按钮)
自定义UITabBar替换系统默认的,目的是为了在UITabBar中间位置添加一个“+号按钮”,下面我们来聊聊具体的实现。
1、自定义WBTabBar,让其继承自UITabBar,代码如下:
//
// WBTabBar.h
// SinaWeibo
//
// Created by android_ls on 15/5/21.
// Copyright (c) 2015年 android_ls. All rights reserved.
// #import <UIKit/UIKit.h> @interface WBTabBar : UITabBar @end
2、tabBar是UITabBarController的只读成员变量(属性),是不让修改的,在UITabBarController.h文件中的声明如下:
@property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0);
针对于这种情况,我们可以使用KVC的方式,更换系统自带的UITabBar,实现代码如下:
WBTabBar *tabBar = [[WBTabBar alloc] init];
[self setValue:tabBar forKeyPath:@"tabBar"];
3、添加一个UIButton到WBTabBar中,实现代码如下:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 添加一个按钮到tabbar中
UIButton *plusBtn = [[UIButton alloc] init];
[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_background_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;
}
4、设置加号按钮的位置,调整WBTabBar中各个UITabBarButton的位置和宽度,具体实现代码如下:
- (void)layoutSubviews
{
[super layoutSubviews]; // 1.设置加号按钮的位置
self.plusBtn.centerX = self.width * 0.5;
self.plusBtn.centerY = self.height * 0.5; // 2.设置其它UITabBarButton的位置和尺寸
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++;
}
}
}
}
5、定义WBTabBarDelegate协议,声明WBTabBar的代理,代码如下:
//
// WBTabBar.h
// SinaWeibo
//
// Created by android_ls on 15/5/21.
// Copyright (c) 2015年 android_ls. All rights reserved.
// #import <UIKit/UIKit.h> #pragma mark 因为在UITabBar中已经声明过一个UITabBarDelegate协议,
#pragma mark 我们若想新增一个对外的代理函数,可以让我们自定义的协议继承自UITabBarDelegate,添加一个扩展函数。 @class WBTabBar; @protocol WBTabBarDelegate <UITabBarDelegate> @optional
- (void)tabBarDidClickPlusButton:(WBTabBar *)tabBar; @end @interface WBTabBar : UITabBar @property (nonatomic, weak) id<WBTabBarDelegate> tabBarDelegate; @end
6、在加号按钮的点击事件处理器中,通知代理
#pragma mark 加号按钮点击事件处理器
- (void)plusClick
{
// 通知代理
if ([self.tabBarDelegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
[self.tabBarDelegate tabBarDidClickPlusButton:self];
}
}
7、在WBTabBarController中设置WBTabBar的代理,具体实现如下:
// 2、使用KVC的方式,更换系统自带的UITabBar
WBTabBar *tabBar = [[WBTabBar alloc] init];
tabBar.tabBarDelegate = self;
[self setValue:tabBar forKeyPath:@"tabBar"];
#pragma mark - HWTabBarDelegate代理方法
- (void)tabBarDidClickPlusButton:(WBTabBar *)tabBar
{
ComposeViewController *composeViewController= [[ComposeViewController alloc] init];
UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:composeViewController];
[self presentViewController:navigationController animated:YES completion:nil];
}
iOS之自定义UITabBar替换系统默认的(添加“+”号按钮)的更多相关文章
- iOS项目——自定义UITabBar与布局
在上一篇文章iOS项目——基本框架搭建中,我们详细说明了如何对TabBarItem的图片属性以及文字属性进行一些自定义配置.但是,很多时候,我们需要修改TabBarItem的图片和文字属性之外,还需要 ...
- iOS成长之路-使用系统默认声音、震动
导入框架 代码片段 apple系统默认声音名称说明: 1.声音格式是MP3或m4r的需要转成caf格式(可先转成aif , aiff,然后修改后缀) 2.路径在/System/Library/Audi ...
- django 自定义用户表替换系统默认表
首先新建一个users应用,编写这个应用的models类. from django.contrib.auth.models import AbstractUser class UserProfile( ...
- ExpandableListView(一)替换系统默认的箭头
很多朋友可能在android开发中,用过ExpandableListView这个组件,这个组件功能强大,比传统的ListView有好多优势.然而在开发中,我相信有好多人,包括我个人都会遇到下面的一些问 ...
- Android系统默认对话框添加图片
开发工具Android Studio 今天公司UI要求软件对话框改成加图片的,以前没有做过,所以就学习了一下,废话不多说, 看效果: 创建XML文件dialog_lsit_item.xml <L ...
- [RK3288][Android6.0] 调试笔记 --- 替换系统签名【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/55100299 Platform: RK3288OS: Android 6.0Kernel: ...
- SharePoint 2010 使用自定义aspx页面替换列表默认的新建(NewForm.aspx),查看(DispForm.aspx)和编辑(EditForm.aspx)页面
转:http://www.cnblogs.com/sygwin/archive/2011/11/04/2236678.html 如何使用自定义的aspx页(比如Application Page)替换列 ...
- 去除android或者iOS系统默认的一些样式总结
ios系统中元素被触摸时产生的半透明灰色遮罩怎么去掉 iOS用户点击一个链接,会出现一个半透明灰色遮罩, 如果想要禁用,可设置-webkit-tap-highlight-color的alpha值为0, ...
- 将Windows系统默认的Administrator帐号改名为我们自定义的名称
将Windows系统默认的Administrator帐号改名为我们自定义的名称.. ---------如何将Administrator帐号改名为我们自定义的名称:Win+R--->>输入g ...
随机推荐
- Spring @Resource、@Autowired、@Qualifier的注解注入及区别
spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...
- 简易博客编辑器:玩转document.execCommand命令
xhEditor是基于jQuery开发的跨平台轻量可视化XHTML编辑器,基于LGPL开源协议发布.今天就把它山寨一下. 上几张图,看看效果: 先做出菜单部分:发现是一张背景图片,所以用图片映射的方法 ...
- bzoj 1411 [ZJOI2009]硬币游戏
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1411 [题意] N个硬币放在一个有2*N个位置的圆桌上,求T次操作后的情况.对于一个操 ...
- sql-定义变量
declare @subject nvarchar(50) set @subject=(select Subject from dbo.Scores where ID=1) --select @sub ...
- 50道经典的JAVA编程题(41-45)
50道经典的JAVA编程题(41-45),苦逼的程序猿,晚上睡不着了编程吧~今天坚持做10道题!发现编程能是我快乐...O(∩_∩)O哈哈~能平静我烦乱的心,剩下5道题留到考试完了再做吧!该睡觉了.. ...
- python PIL下的各种问题
为了实现验证码的功能,使用了PIL.结果出现各种问题: 先是"ImportError: The _imagingft C module is not installed",goog ...
- HW6.28
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- hdoj 1878 欧拉回路
欧拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 如何检查机器是否因为装了Windows更新而需要重新启动
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何检查机器是否因为装了Windows更新而需要重新启动.
- Why Does Everyone Else Appear to Be Succeeding?
Why Does Everyone Else Appear to Be Succeeding? —Steven G. Krantz When you are a student, it will a ...