iOS 自定制TabBar(中间是一个大按钮)

来源:互联网 发布:java red5 编辑:程序博客网 时间:2024/03/28 18:04

许多APP中都有这个效果,点击中间的大按钮就会present一个页面出来,关闭这个页面就返回到之前的页面
这个功能看起来很神奇,其实实现起来也相当简单
实现方式:创建一个类继承自UITabBar,在view上添加一个大的按钮 放到中间,然后需要写一个代理 用来实现按钮的点击事件
效果如下图:github地址https://github.com/ColdChains/LAXTabBarController
这里写图片描述

这是用Swift写的demo
一、创建自定制TabBar的类
二、创建协议,声明点击按钮的事件
三、在按钮点击事件里调用代理方法

////  LAXTabBar.swift//  MeiLiTV////  Created by 冰凉的枷锁 on 2017/3/9.//  Copyright © 2017年 冰凉的枷锁. All rights reserved.//import UIKitprotocol LAXTabBarDelegate : UITabBarDelegate {    func tabBarDidClickPlusButton(_: LAXTabBar)}class LAXTabBar: UITabBar {    init() {        super.init(frame: CGRect.init(x: 0, y: ScreenHeight - 49, width: ScreenWidth, height: 49))        plusButton.center = CGPoint.init(x: ScreenWidth / 2, y: 49 - plusButton.bounds.size.height / 2)        self.addSubview(plusButton)    }    convenience init(delegate: LAXTabBarDelegate) {        self.init()        self.tabbarDelegate = delegate    }    required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }    lazy var plusButton: UIButton = {        let btn = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 55, height: 55))        btn.setImage(UIImage.init(named: PlusButtonImage), for: .normal)        btn.addTarget(self, action: #selector(plusButtonAction(sender:)), for: .touchUpInside)        return btn    }()    var tabbarDelegate: LAXTabBarDelegate?    func plusButtonAction(sender: UIButton) {        self.tabbarDelegate?.tabBarDidClickPlusButton(self)    }}

四、创建一个类,继承UITabBarController,设置tabbar为自定制的tabbar
五、实现协议方法

class RootViewController: UITabBarController, LAXTabBarDelegate {    override func viewDidLoad() {        super.viewDidLoad()        let tabBar = LAXTabBar.init(delegate: self)        self.setValue(tabBar, forKey: "tabBar")        self.tabBar.barTintColor = TabBarColor        self.tabBar.tintColor = OrangeColor        if #available(iOS 10.0, *) {            self.tabBar.unselectedItemTintColor = WhiteColor        } else {            // Fallback on earlier versions        }    }    func tabBarDidClickPlusButton(_: LAXTabBar) {        let vc = LiveViewController()        self.present(vc, animated: true, completion: nil)    }}

最后在控制器中添加五个个页面,第三个item只是一个占位的作用,不要设置title和image。这里的代码我就不写了,想看效果的去github上下载吧https://github.com/ColdChains/LAXTabBarController

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 烤箱烤红薯没有锡纸怎么办 烤箱烤羊肉串滴油怎么办 黄金虎嘴脱臼了怎么办 孕妇吃了马头鱼怎么办 慈鲷鱼生完小鱼怎么办 买的烤鱼片刺多怎么办 鸡蛋不太新鲜了怎么办 麻雀从巢里掉下来怎么办 小鱼生了鱼蛋怎么办 吃了没熟透的鱼怎么办 吃了变质的虾怎么办 吃了不新鲜的肉怎么办 吃不新鲜的虾怎么办 鸡胸肉不新鲜了怎么办 吃了不新鲜的鱼怎么办 生的猪肉有点臭怎么办? 猪肉馅不新鲜了怎么办 买的肉有点臭了怎么办 炸的东西不脆了怎么办 油炸东西回软了怎么办 吃石斑鱼蛋吐了怎么办 家里的烟筒堵了怎么办 脖子上长鸡皮肤怎么办 铁板烤蔬菜粘锅怎么办 残余尿量300ml怎么办 肌肉拉伤怎么办恢复快小腿 睡觉把背扭了怎么办 后背一侧扭筋了怎么办 背部的筋扭到了怎么办 跳绳跳得膝盖疼怎么办 跑步小腿变粗了怎么办 一蹲下膝盖就响怎么办 做深蹲时膝盖总是吱吱响怎么办 爬山爬的膝盖疼怎么办 膝盖一吹风就疼怎么办 走路太多膝盖腿疼怎么办 膝盖一着凉就痛怎么办 月子里脚受凉了怎么办 膝关节受凉少量积液发胀怎么办 刮痧后吹空调了怎么办 200斤胖子膝盖痛怎么办