---恢复内容开始---

#### 封装了一个带有中间凸起的自定制Tabbar,包含4个普通按钮和中间的一个凸起按钮-

  1. 首先封装了一个UIButton,重新设置了UIButton的图片位置和label位置
  2. 使用便利构造器创建了一个带有imageview的构造方法,用来构造中间特殊的按钮
  3. 继承与UIView创建了一个自定制tabbar类,大小为屏幕宽度和49 高,
  4. 动态创建5个自定制的UIButton,对中间的按钮做了特殊处理,其中的位置大小可以根据需求设置。
  5. 设置一个全局的button存储高亮状态下的按钮
  6. 使用闭包进行了控制器于自定制tabbar之间的传值,实现了不同按钮切换不同界面的功能

使用方法:

  1. 实例化一个自定制TabBar let myTabbar = ZYF_Main_MyTabBar()
  2. 设置自定制TabBar的frame myTabbar.frame = CGRectMake(0, height - 49, width, 49)
  3. 调用方法,传入参数:标题数组、.Normal状态下的图片数组、.selected状态下的图片数组,每个按钮之间的间距

    tabbar.creatTabBar(title, imageNames: imageName, selectedImageNames: selectedImage, space: 83)

Github地址

上代码

封装UIButton,重置UIButton的图片位置和Label位置

class ZYF_MyTabBarButton: UIButton {
//重写构造方法
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = CGRectMake(0, 0, 49, 49)
self.setTitleColor(UIColor.grayColor(), forState: .Normal)
self.setTitleColor(UIColor.redColor(), forState: .Selected)
self.titleLabel?.font = UIFont.systemFontOfSize(11)
self.titleLabel?.textAlignment = .Center
} required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//重写button中图片的位置
override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
return CGRectMake((contentRect.size.width - 30) / 2, 2, 30, 30)
}
//重写button中文本框的位置
override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
return CGRectMake(0, contentRect.size.height - 17, contentRect.size.width, 15)
} //使用便利构造器构造中间特殊按钮
convenience init(frame: CGRect,image:String) {
self.init(frame:frame)
let imageView = UIImageView(frame: CGRectMake(0,0,70,70))
imageView.image = UIImage(named: image)
self.addSubview(imageView)
}
}

继承UIView制作MyTabBar

class ZYF_Main_MyTabBar: UIView {
//设置一个全局的button存储selected按钮
var button = UIButton()
//获得屏高
let height = UIScreen.mainScreen().bounds.size.height
//获得屏宽
let width = UIScreen.mainScreen().bounds.size.width //闭包传值,创建一个闭包,用来传递被选中的按钮,以实现页面的转换
var clickBlock:((selcted:Int) ->())? //重写构造方法
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = CGRectMake(0, 0, width, 49)
self.backgroundColor = UIColor.blackColor()
//打开用户交互
self.userInteractionEnabled = true
} required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//写一个制作方法,传图标题数组、图片名称数组、被选中状态下图片名称数组和每个按钮之间的间距
func creatTabBar(titNames:[String],imageNames:[String],selectedImageNames:[String],space:Int) {
//快速布局
for i in 0...titNames.count - 1 {
var btn = ZYF_MyTabBarButton(type: .Custom)
btn.frame = CGRectMake(20 + CGFloat(i) * 83, 0, 49, 49)
let image = UIImage(named: imageNames[i])
let selectedImage = UIImage(named: selectedImageNames[i]) //Mark*设置中间特殊按钮
if i == 2{ //Mark* 如果想设置凸起的话让pointY为负值 let pointY:CGFloat = 5
let pointX:CGFloat = 49 + abs(pointY)
btn = ZYF_MyTabBarButton.init(frame: frame, image: "ZYF-Login-Dou")
btn.frame = CGRectMake(183, pointY, width / 5, pointX)
} else {
btn.setImage(selectedImage, forState: .Selected)
btn.setImage(image, forState: .Normal)
} btn.setTitle(titNames[i], forState: .Normal)
self.addSubview(btn)
btn.tag = 10 + i
//为每个btn添加点击事件,以实现界面替换
btn.addTarget(self, action: #selector(self.btnClick(_:)), forControlEvents: .TouchUpInside) //设置默认第一个按钮为选中状态
if i == 0 {
btn.selected = true
self.button = btn
}
}
} //点击事件
func btnClick(sender:UIButton) {
//实现视图切换
print("视图切换")
//通过tag值获取点击的btn
let index = sender.tag - 10
if index < 2 {
//设置闭包中的值
if clickBlock != nil {
clickBlock!(selcted:index)
print("index<2")
}
} else if index > 2 {
if clickBlock != nil {
clickBlock!(selcted:index - 1)
}
} else {
clickBlock!(selcted:999)
return
} //设置选中按钮
self.button.selected = false
sender.selected = true
self.button = sender
}
}

使用方式


//获取屏宽
let width = UIScreen.mainScreen().bounds.size.width
//获取屏高
let height = UIScreen.mainScreen().bounds.size.height
//实例化自定制TabBar
let tabbar = ZYF_Main_MyTabBar()
//隐藏系统的tabbar
self.tabBar.hidden = true
//设置位置
tabbar.frame = CGRectMake(0, height - 49, width, 49)
//标题数组
let title = ["发现","关注","","消息","我的"] //图片名称数组
let imageName = ["find","focus","center","message","contact"] //选中图片名称数组
let selectedImage = ["sfind","sfocs","center","smessage","smy"] //创建按钮
tabbar.creatTabBar(title, imageNames: imageName, selectedImageNames: selectedImage, space: 83) //使用闭包中的值
tabbar.clickBlock = {(selcted:Int) in
if selcted == 999 {
print("点击了特殊按钮")
} else {
print(123)
self.selectedIndex = selcted
}
}
//将自定制tabbar加到主视图上
self.view.addSubview(tabbar)

效果图如下:

---恢复内容结束---

---恢复内容开始---

#### 封装了一个带有中间凸起的自定制Tabbar,包含4个普通按钮和中间的一个凸起按钮-

  1. 首先封装了一个UIButton,重新设置了UIButton的图片位置和label位置
  2. 使用便利构造器创建了一个带有imageview的构造方法,用来构造中间特殊的按钮
  3. 继承与UIView创建了一个自定制tabbar类,大小为屏幕宽度和49 高,
  4. 动态创建5个自定制的UIButton,对中间的按钮做了特殊处理,其中的位置大小可以根据需求设置。
  5. 设置一个全局的button存储高亮状态下的按钮
  6. 使用闭包进行了控制器于自定制tabbar之间的传值,实现了不同按钮切换不同界面的功能

使用方法:

  1. 实例化一个自定制TabBar let myTabbar = ZYF_Main_MyTabBar()
  2. 设置自定制TabBar的frame myTabbar.frame = CGRectMake(0, height - 49, width, 49)
  3. 调用方法,传入参数:标题数组、.Normal状态下的图片数组、.selected状态下的图片数组,每个按钮之间的间距

    tabbar.creatTabBar(title, imageNames: imageName, selectedImageNames: selectedImage, space: 83)

Github地址

上代码

封装UIButton,重置UIButton的图片位置和Label位置

class ZYF_MyTabBarButton: UIButton {
//重写构造方法
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = CGRectMake(0, 0, 49, 49)
self.setTitleColor(UIColor.grayColor(), forState: .Normal)
self.setTitleColor(UIColor.redColor(), forState: .Selected)
self.titleLabel?.font = UIFont.systemFontOfSize(11)
self.titleLabel?.textAlignment = .Center
} required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//重写button中图片的位置
override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
return CGRectMake((contentRect.size.width - 30) / 2, 2, 30, 30)
}
//重写button中文本框的位置
override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
return CGRectMake(0, contentRect.size.height - 17, contentRect.size.width, 15)
} //使用便利构造器构造中间特殊按钮
convenience init(frame: CGRect,image:String) {
self.init(frame:frame)
let imageView = UIImageView(frame: CGRectMake(0,0,70,70))
imageView.image = UIImage(named: image)
self.addSubview(imageView)
}
}

继承UIView制作MyTabBar

class ZYF_Main_MyTabBar: UIView {
//设置一个全局的button存储selected按钮
var button = UIButton()
//获得屏高
let height = UIScreen.mainScreen().bounds.size.height
//获得屏宽
let width = UIScreen.mainScreen().bounds.size.width //闭包传值,创建一个闭包,用来传递被选中的按钮,以实现页面的转换
var clickBlock:((selcted:Int) ->())? //重写构造方法
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = CGRectMake(0, 0, width, 49)
self.backgroundColor = UIColor.blackColor()
//打开用户交互
self.userInteractionEnabled = true
} required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//写一个制作方法,传图标题数组、图片名称数组、被选中状态下图片名称数组和每个按钮之间的间距
func creatTabBar(titNames:[String],imageNames:[String],selectedImageNames:[String],space:Int) {
//快速布局
for i in 0...titNames.count - 1 {
var btn = ZYF_MyTabBarButton(type: .Custom)
btn.frame = CGRectMake(20 + CGFloat(i) * 83, 0, 49, 49)
let image = UIImage(named: imageNames[i])
let selectedImage = UIImage(named: selectedImageNames[i]) //Mark*设置中间特殊按钮
if i == 2{ //Mark* 如果想设置凸起的话让pointY为负值 let pointY:CGFloat = 5
let pointX:CGFloat = 49 + abs(pointY)
btn = ZYF_MyTabBarButton.init(frame: frame, image: "ZYF-Login-Dou")
btn.frame = CGRectMake(183, pointY, width / 5, pointX)
} else {
btn.setImage(selectedImage, forState: .Selected)
btn.setImage(image, forState: .Normal)
} btn.setTitle(titNames[i], forState: .Normal)
self.addSubview(btn)
btn.tag = 10 + i
//为每个btn添加点击事件,以实现界面替换
btn.addTarget(self, action: #selector(self.btnClick(_:)), forControlEvents: .TouchUpInside) //设置默认第一个按钮为选中状态
if i == 0 {
btn.selected = true
self.button = btn
}
}
} //点击事件
func btnClick(sender:UIButton) {
//实现视图切换
print("视图切换")
//通过tag值获取点击的btn
let index = sender.tag - 10
if index < 2 {
//设置闭包中的值
if clickBlock != nil {
clickBlock!(selcted:index)
print("index<2")
}
} else if index > 2 {
if clickBlock != nil {
clickBlock!(selcted:index - 1)
}
} else {
clickBlock!(selcted:999)
return
} //设置选中按钮
self.button.selected = false
sender.selected = true
self.button = sender
}
}

使用方式


//获取屏宽
let width = UIScreen.mainScreen().bounds.size.width
//获取屏高
let height = UIScreen.mainScreen().bounds.size.height
//实例化自定制TabBar
let tabbar = ZYF_Main_MyTabBar()
//隐藏系统的tabbar
self.tabBar.hidden = true
//设置位置
tabbar.frame = CGRectMake(0, height - 49, width, 49)
//标题数组
let title = ["发现","关注","","消息","我的"] //图片名称数组
let imageName = ["find","focus","center","message","contact"] //选中图片名称数组
let selectedImage = ["sfind","sfocs","center","smessage","smy"] //创建按钮
tabbar.creatTabBar(title, imageNames: imageName, selectedImageNames: selectedImage, space: 83) //使用闭包中的值
tabbar.clickBlock = {(selcted:Int) in
if selcted == 999 {
print("点击了特殊按钮")
} else {
print(123)
self.selectedIndex = selcted
}
}
//将自定制tabbar加到主视图上
self.view.addSubview(tabbar)

效果图如下:

---恢复内容结束---

#### 封装了一个带有中间凸起的自定制Tabbar,包含4个普通按钮和中间的一个凸起按钮-

  1. 首先封装了一个UIButton,重新设置了UIButton的图片位置和label位置
  2. 使用便利构造器创建了一个带有imageview的构造方法,用来构造中间特殊的按钮
  3. 继承与UIView创建了一个自定制tabbar类,大小为屏幕宽度和49 高,
  4. 动态创建5个自定制的UIButton,对中间的按钮做了特殊处理,其中的位置大小可以根据需求设置。
  5. 设置一个全局的button存储高亮状态下的按钮
  6. 使用闭包进行了控制器于自定制tabbar之间的传值,实现了不同按钮切换不同界面的功能

使用方法:

  1. 实例化一个自定制TabBar let myTabbar = ZYF_Main_MyTabBar()
  2. 设置自定制TabBar的frame myTabbar.frame = CGRectMake(0, height - 49, width, 49)
  3. 调用方法,传入参数:标题数组、.Normal状态下的图片数组、.selected状态下的图片数组,每个按钮之间的间距

    tabbar.creatTabBar(title, imageNames: imageName, selectedImageNames: selectedImage, space: 83)

Github地址

上代码

封装UIButton,重置UIButton的图片位置和Label位置

class ZYF_MyTabBarButton: UIButton {
//重写构造方法
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = CGRectMake(0, 0, 49, 49)
self.setTitleColor(UIColor.grayColor(), forState: .Normal)
self.setTitleColor(UIColor.redColor(), forState: .Selected)
self.titleLabel?.font = UIFont.systemFontOfSize(11)
self.titleLabel?.textAlignment = .Center
} required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//重写button中图片的位置
override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
return CGRectMake((contentRect.size.width - 30) / 2, 2, 30, 30)
}
//重写button中文本框的位置
override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
return CGRectMake(0, contentRect.size.height - 17, contentRect.size.width, 15)
} //使用便利构造器构造中间特殊按钮
convenience init(frame: CGRect,image:String) {
self.init(frame:frame)
let imageView = UIImageView(frame: CGRectMake(0,0,70,70))
imageView.image = UIImage(named: image)
self.addSubview(imageView)
}
}

继承UIView制作MyTabBar

class ZYF_Main_MyTabBar: UIView {
//设置一个全局的button存储selected按钮
var button = UIButton()
//获得屏高
let height = UIScreen.mainScreen().bounds.size.height
//获得屏宽
let width = UIScreen.mainScreen().bounds.size.width //闭包传值,创建一个闭包,用来传递被选中的按钮,以实现页面的转换
var clickBlock:((selcted:Int) ->())? //重写构造方法
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = CGRectMake(0, 0, width, 49)
self.backgroundColor = UIColor.blackColor()
//打开用户交互
self.userInteractionEnabled = true
} required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//写一个制作方法,传图标题数组、图片名称数组、被选中状态下图片名称数组和每个按钮之间的间距
func creatTabBar(titNames:[String],imageNames:[String],selectedImageNames:[String],space:Int) {
//快速布局
for i in 0...titNames.count - 1 {
var btn = ZYF_MyTabBarButton(type: .Custom)
btn.frame = CGRectMake(20 + CGFloat(i) * 83, 0, 49, 49)
let image = UIImage(named: imageNames[i])
let selectedImage = UIImage(named: selectedImageNames[i]) //Mark*设置中间特殊按钮
if i == 2{ //Mark* 如果想设置凸起的话让pointY为负值 let pointY:CGFloat = 5
let pointX:CGFloat = 49 + abs(pointY)
btn = ZYF_MyTabBarButton.init(frame: frame, image: "ZYF-Login-Dou")
btn.frame = CGRectMake(183, pointY, width / 5, pointX)
} else {
btn.setImage(selectedImage, forState: .Selected)
btn.setImage(image, forState: .Normal)
} btn.setTitle(titNames[i], forState: .Normal)
self.addSubview(btn)
btn.tag = 10 + i
//为每个btn添加点击事件,以实现界面替换
btn.addTarget(self, action: #selector(self.btnClick(_:)), forControlEvents: .TouchUpInside) //设置默认第一个按钮为选中状态
if i == 0 {
btn.selected = true
self.button = btn
}
}
} //点击事件
func btnClick(sender:UIButton) {
//实现视图切换
print("视图切换")
//通过tag值获取点击的btn
let index = sender.tag - 10
if index < 2 {
//设置闭包中的值
if clickBlock != nil {
clickBlock!(selcted:index)
print("index<2")
}
} else if index > 2 {
if clickBlock != nil {
clickBlock!(selcted:index - 1)
}
} else {
clickBlock!(selcted:999)
return
} //设置选中按钮
self.button.selected = false
sender.selected = true
self.button = sender
}
}

使用方式


//获取屏宽
let width = UIScreen.mainScreen().bounds.size.width
//获取屏高
let height = UIScreen.mainScreen().bounds.size.height
//实例化自定制TabBar
let tabbar = ZYF_Main_MyTabBar()
//隐藏系统的tabbar
self.tabBar.hidden = true
//设置位置
tabbar.frame = CGRectMake(0, height - 49, width, 49)
//标题数组
let title = ["发现","关注","","消息","我的"] //图片名称数组
let imageName = ["find","focus","center","message","contact"] //选中图片名称数组
let selectedImage = ["sfind","sfocs","center","smessage","smy"] //创建按钮
tabbar.creatTabBar(title, imageNames: imageName, selectedImageNames: selectedImage, space: 83) //使用闭包中的值
tabbar.clickBlock = {(selcted:Int) in
if selcted == 999 {
print("点击了特殊按钮")
} else {
print(123)
self.selectedIndex = selcted
}
}
//将自定制tabbar加到主视图上
self.view.addSubview(tabbar)

效果图如下:

Swift-自定制带有特殊按钮TabBar的更多相关文章

  1. ios定制中间突出的tabBar

    我觉得有两个思路,一个是自己写tabBar  通过自定义实现,缺点呢就是比较麻烦,优点就是代码比较清楚,而且比较稳定. 另一个思路就是写个大按钮加在tabBar上 通过监听tabitem的点击来实现相 ...

  2. 定制Android透明按钮

    自己在学习和做例子的过程中,常常会需要按钮,由于系统自带按钮样式不太好看,所以需要我们自己来定制项目得按钮,我常常采用2中方法: 1.是制作9-patch的图片,这样能够匹配文字内容的长短. 2.是指 ...

  3. Android自定义控件实现带有清除按钮的EditText

    首先声明我也是参考了别人的思路,只是稍微做了下修改,增加显示密码与隐藏密码,没有输入字符串时让EditText进行抖动,废话少说这里附上效果图 效果很赞有木有 那么怎么实现这种效果呢?那就跟着我一起来 ...

  4. Swift - 修改导航栏“返回”按钮文字,图标

    Swift - 修改导航栏“返回”按钮文字,图标 2015-11-27 09:13发布:hangge浏览:4037   项目中常常会使用 UINavigationController 对各个页面进行导 ...

  5. Swift - 修改导航栏“返回”按钮文字和图标 /手势冲突解决/响应范围

    iOS11之前 修改导航栏“返回”按钮文字,图标 https://blog.csdn.net/u012701023/article/details/50264265 iOS11 完美解决导航栏按钮偏移 ...

  6. Android自定义View带有删除按钮的EditText

    转载请注明出处http://blog.csdn.net/xiaanming/article/details/11066685 今天给大家带来一个很实用的小控件ClearEditText,就是在Andr ...

  7. Swift 设置navigation左右两侧按钮

    我们以设置右侧按钮为例,左侧方法类似 方法一,直接自定义文字 let item=UIBarButtonItem(title: "分享", style: UIBarButtonIte ...

  8. Swift - 重写导航栏返回按钮

    // 重写导航栏返回按钮方法 func configBackBtn() -> Void { // 返回按钮 let backButton = UIButton(type: .custom) // ...

  9. Swift 统计项目中所有按钮的点击次数

    class Swizzle: NSObject { override class func load() { UIButton.xxx_swizzleSendAction() } } extensio ...

随机推荐

  1. Python 类的方法,下划线有什么不同?

    问: 1,以一个下划线开头的命名 ,如_getFile2,以两个下划线开头的命名 ,如__filename3,以两个下划线开头和结尾的命名,如 __init__()4,其它这些命名有什么不同吗? 答:

  2. zabbix通过snmp监控网络设备

    首先需要在zabbix的server端或proxy端安装snmpd服务 安装: yum -y install net-snmp* 查看版本: [root@Check3 ~]# snmpd -v NET ...

  3. 不同vlan间通信的三种配置方式

    1.单臂路由(图) 环境:一台路由器,一台二层交换机,两台pc机 二层交换机的配置 //创建vlan 和 vlan : Switch(config)#vlan Switch(config-vlan)# ...

  4. js 执行跨域刷新页面

    主要代码: 注意这段代码 是子页面中添加的也就是弹出的那个页面刷新父页面 <script type="text/javascript"> function shuaxi ...

  5. WebSocket相关

    原文:http://www.cnblogs.com/jinjiangongzuoshi/p/5062092.html 前言 今天看了一些资料,记录一下心得. websocket是html5引入的一个新 ...

  6. java XML解析

    package com.kpsh.myself; import java.io.File;import java.io.FileInputStream;import java.util.List; i ...

  7. Java条件语句之多重 if

    多重 if 语句,在条件 1 不满足的情况下,才会进行条件 2 的判断:当前面的条件均不成立时,才会执行 else 块内的代码.例如,如果考试成绩大于 90 分,则奖励一个 IPHONE 5S ,如果 ...

  8. Linux常用命令.rpm

    1.安装: rpm -ivh 包全名(查询依赖网址:http://www.rpmfind.net) -i(install):安装 -v(verbose):显示详细信息 -h(hash):显示进度 -- ...

  9. thrift安装及常见问题

    一.安装thrift (macOS / Linux) 1. 下载thrift0.10.0源码 https://github.com/apache/thrift/releases/tag/0.10.0 ...

  10. LeetCode第[16]题(Java):3Sum Closest (和目标值最接近的三个数的和)——Medium

    题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...