swift之tabBarController

=================简单的tabbarcontroller     

 

import UIKit

class LYBTabBarVC: UITabBarController {

    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor=UIColor.white

        UITabBar.appearance().isTranslucent=false //去掉半透明

//        self.tabBar.barTintColor=UIColor.white//tabbar背景色

//        UITabBar.appearance().insertSubview(UIView.init(), at: 0)//插入一层背景,相当于改变tabar背景色

 // self.tabBar.backgroundImage=UIImage.init()//tabbar透明,必须 self.tabBar.isTranslucent=true时才有效

    //    self.tabBar.shadowImage=UIImage.init() //去掉tabbar顶部的黑线

        self.addChildcontrollerWitnImageAndtitle(childvc: LYBHomeVC.init(), image: UIImage.init(named: "home")!, selectImage: UIImage.init(named: "home")!, title: "首页", titleColor: UIColor.white, selectTitleColor: UIColor.black)

      

        self.addChildcontrollerWitnImageAndtitle(childvc: LYBHomeVC.init(), image: UIImage.init(named: "info")!, selectImage: UIImage.init(named: "info")!, title: "消息", titleColor: UIColor.white, selectTitleColor: UIColor.black)

        self.addChildcontrollerWitnImageAndtitle(childvc: LYBHomeVC.init(), image: UIImage.init(named: "other")!, selectImage: UIImage.init(named: "other")!, title: "Other", titleColor: UIColor.white, selectTitleColor: UIColor.black)

        self.addChildcontrollerWitnImageAndtitle(childvc: LYBHomeVC.init(), image: UIImage.init(named: "luntan")!, selectImage: UIImage.init(named: "luntan")!, title: "论坛", titleColor: UIColor.white, selectTitleColor: UIColor.black)

        

        self.addChildcontrollerWitnImageAndtitle(childvc: LYBHomeVC.init(), image: UIImage.init(named: "my")!, selectImage: UIImage.init(named: "my")!, title: "我的", titleColor: UIColor.white, selectTitleColor: UIColor.black)

    }

    

    private func addChildcontrollerWitnImageAndtitle(childvc:UIViewController,image:UIImage,selectImage:UIImage,title:String,titleColor:UIColor,selectTitleColor:UIColor){

        childvc.title=title

        let nav=LYBBaseNav.init(rootViewController: childvc)

        nav.tabBarItem.title=title

        nav.tabBarItem.image=image.withRenderingMode(.alwaysOriginal)

        nav.tabBarItem.selectedImage=selectImage.withRenderingMode(.alwaysOriginal)

        nav.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: titleColor], for:UIControl.State.normal)

        nav.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: selectTitleColor], for:UIControl.State.selected)

        self.addChild(nav)

    }

}

        

=====原生tabBar,修改tabBarItem的中间图片位置

import UIKit

class LYBTabBarVC: UITabBarController {

    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor=UIColor.white

        UITabBar.appearance().isTranslucent=false //去掉半透明

//        self.tabBar.barTintColor=UIColor.white//tabbar背景色

//        UITabBar.appearance().insertSubview(UIView.init(), at: 0)//插入一层背景,相当于改变tabar背景色

        self.addChildcontrollerWitnImageAndtitle(childvc: LYBHomeVC.init(), image: UIImage.init(named: "home")!, selectImage: UIImage.init(named: "home")!.withRenderingMode(.alwaysOriginal), title: "首页", titleColor: UIColor.white, selectTitleColor: UIColor.black)

        self.addChildcontrollerWitnImageAndtitle(childvc: LYBHomeVC.init(), image: UIImage.init(named: "other")!, selectImage: UIImage.init(named: "other")!.withRenderingMode(.alwaysOriginal), title: "Other", titleColor: UIColor.white, selectTitleColor: UIColor.black)

         self.addChildcontrollerWitnImageAndtitle(childvc: LYBInfoVC.init(), image: UIImage.init(named: "info")!, selectImage: UIImage.init(named: "info")!.withRenderingMode(.alwaysOriginal), title: "消息", titleColor: UIColor.white, selectTitleColor: UIColor.black)

        self.addChildcontrollerWitnImageAndtitle(childvc: LYBHomeVC.init(), image: UIImage.init(named: "luntan")!, selectImage: UIImage.init(named: "luntan")!.withRenderingMode(.alwaysOriginal), title: "论坛", titleColor: UIColor.white, selectTitleColor: UIColor.black)

        self.addChildcontrollerWitnImageAndtitle(childvc: LYBMyVC.init(), image: UIImage.init(named: "my")!, selectImage: UIImage.init(named: "my")!.withRenderingMode(.alwaysOriginal), title: "我的", titleColor: UIColor.white, selectTitleColor: UIColor.black)

    }

    private func addChildcontrollerWitnImageAndtitle(childvc:UIViewController,image:UIImage,selectImage:UIImage,title:String,titleColor:UIColor,selectTitleColor:UIColor){

           childvc.title=title

        let nav=LYBBaseNav.init(rootViewController: childvc)

        nav.tabBarItem.title=title

       /*

         改变中间图片位置

         */

        if (childvc.isKind(of:LYBInfoVC.self))

          {

         //直接修改图片的偏移,这里必须设置nav的tabBaritem才有效

        nav.tabBarItem.imageInsets=UIEdgeInsets.init(top: -22, left: 0, bottom: 22, right: 0)

    print("\(childvc)")

        }

        nav.tabBarItem.image=image.withRenderingMode(.alwaysOriginal)

        nav.tabBarItem.selectedImage=selectImage.withRenderingMode(.alwaysOriginal)

        nav.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: titleColor], for:UIControl.State.normal)

        nav.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: selectTitleColor], for:UIControl.State.selected)

        self.addChild(nav)

    }

}

 ======自定义TabBar,采用协议委托的方式

//自定义tabbar

import UIKit

@objc protocol LYBAutodefineTabbardelegate:NSObjectProtocol{

       func centerBtnclick()

}

class LYBAutodefineTbaBar: UITabBar{

       weak var delegates: LYBAutodefineTabbardelegate?

    var  btn = UIButton.init(frame: CGRect.init(x: 0, y:0, width: 100, height: 100))//这里只控制宽高

    override init(frame: CGRect) {

        super.init(frame: frame)

        self.backgroundColor=UIColor.purple

        btn.setImage(UIImage.init(named: "center"), for: UIControl.State.normal)

//        btn.setTitle("中心", for: UIControl.State.normal)

//        btn.setTitleColor(UIColor.red, for: UIControl.State.normal)

        btn.addTarget(self, action: #selector(cenbtnclick), for: UIControl.Event.touchUpInside)

   //如果同时设置图片和文字,需要调整图片和文字的位置,否则有可能文字显示不出来

        self.addSubview(btn)

    }

    @objc func cenbtnclick(){

if(( self.delegates?.responds(to: Selector(("centerBtnclick")))) != nil){

        //调用代理方法

         self.delegates?.centerBtnclick()

        }

    }

    

    required init?(coder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    

    override func layoutSubviews() {

        super.layoutSubviews()

        // 1.设置中间按钮的位置

        var centerBtnTemp=btn.center

        centerBtnTemp.x=self.frame.size.width/2

        //这个控制按钮图片上下位置

        centerBtnTemp.y=self.frame.size.height/2-20

        btn.center=centerBtnTemp

        

        // 2.设置其它UITabBarButton的位置和尺寸

        let otherBtnwidth=self.frame.size.width/3        //底部有5个按钮就除以5,底部有3个按钮就除以3

        var index=0

        for child in self.subviews{

            let clas: AnyClass?=NSClassFromString("UITabBarButton")

            if child.isKind(of: clas!){

                var otherBtnTemp=child.frame

                otherBtnTemp.size.width=otherBtnwidth

                otherBtnTemp.origin.x=CGFloat(index)*otherBtnwidth

                child.frame=otherBtnTemp

                index+=1

                if index==1 {  //如果底部总共是5个按钮这里就改成index==2,底部有3个按钮,index==1

                    index+=1

                }

            }

        }

    }

}

 

import UIKit

class LYBAutodefineTabBarVc: UITabBarController,LYBAutodefineTabbardelegate {

    //代理方法的实现

    func centerBtnclick() {

        print("代理01")

    }

    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor=UIColor.white

        let autodefineTabBar=LYBAutodefineTbaBar()

        autodefineTabBar.delegates=self

        self.setValue(autodefineTabBar, forKey: "tabBar")

        self.addChildcontrollerWitnImageAndtitle(childvc: LYBHomeVC.init(), image: UIImage.init(named: "home")!, selectImage: UIImage.init(named: "home")!.withRenderingMode(.alwaysOriginal), title: "首页", titleColor: UIColor.white, selectTitleColor: UIColor.black)

        self.addChildcontrollerWitnImageAndtitle(childvc: LYBHomeVC.init(), image: UIImage.init(named: "info")!, selectImage: UIImage.init(named: "info")!.withRenderingMode(.alwaysOriginal), title: "消息", titleColor: UIColor.white, selectTitleColor: UIColor.black)

        self.addChildcontrollerWitnImageAndtitle(childvc: LYBHomeVC.init(), image: UIImage.init(named: "luntan")!, selectImage: UIImage.init(named: "luntan")!.withRenderingMode(.alwaysOriginal), title: "论坛", titleColor: UIColor.white, selectTitleColor: UIColor.black)

           self.addChildcontrollerWitnImageAndtitle(childvc: LYBMyVC.init(), image: UIImage.init(named: "my")!, selectImage: UIImage.init(named: "my")!.withRenderingMode(.alwaysOriginal), title: "我的", titleColor: UIColor.white, selectTitleColor: UIColor.black)

    }

    private func addChildcontrollerWitnImageAndtitle(childvc:UIViewController,image:UIImage,selectImage:UIImage,title:String,titleColor:UIColor,selectTitleColor:UIColor){

        childvc.title=title

        let nav=LYBBaseNav.init(rootViewController: childvc)

        nav.tabBarItem.title=title

        nav.tabBarItem.image=image.withRenderingMode(.alwaysOriginal)

        nav.tabBarItem.selectedImage=selectImage.withRenderingMode(.alwaysOriginal)

        nav.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: titleColor], for:UIControl.State.normal)

        nav.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: selectTitleColor], for:UIControl.State.selected)

        self.addChild(nav)

    }

}

去除tabBar顶部黑线:

if #available(iOS 13, *) {
    let appearance = self.tabBar.standardAppearance.copy()
    appearance.shadowImage = UIImage()
    appearance.shadowColor = .clear
    self.tabBar.standardAppearance = appearance
} else {
    self.tabBar.shadowImage = UIImage()
    self.tabBar.backgroundImage = UIImage()
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值