您的当前位置:首页Swift--最简单的方式创建TabBar

Swift--最简单的方式创建TabBar

2024-12-13 来源:哗拓教育

摘要:最近要从零开发一个项目,之前只是在OC上搭建过TabbarViewcontroller,后来转到swift之后也只是看过,趁着现在项目前有点时间,通过一个新的Demo说一下个人认为比较简单的一种创建Tabbar的方式(包括了对Nav的封装)。

一、新建Navigationcontroller.swift和TabbarViewcontroller.swift两个类

1、Navigationcontroller.swift中(继承自UINavigationController)
  • 在Navigationcontroller中写入如下代码:
override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.navigationItem.backBarButtonItem = UIBarButtonItem.init(title: "返回", style: .plain, target: nil, action: nil)
        let navbar = UINavigationBar.appearance()
        navbar.setBackgroundImage(UIImage.init(named: ""), for: .default)
        //按钮默认值
        let tabbarItem = UITabBarItem.appearance()
        let itemTextAttributesnormalDict: Dictionary< String ,AnyObject >  = [NSForegroundColorAttributeName:UIColor.brown,NSFontAttributeName:UIFont.systemFont(ofSize: 20)]
        let itemTextAttributesselectDict: Dictionary< String ,AnyObject >  = [NSForegroundColorAttributeName:UIColor.red,NSFontAttributeName:UIFont.systemFont(ofSize: 20)]
        tabbarItem.setTitleTextAttributes(itemTextAttributesnormalDict, for: .normal)
        tabbarItem.setTitleTextAttributes(itemTextAttributesselectDict, for: .selected)
    }

然后构造一个函数构造器

func navigationControllerWithRootViewController(Vc: UIViewController, image: UIImage, selImage: UIImage) -> UINavigationController {
        let nav = UINavigationController(rootViewController: Vc)
        nav.tabBarItem.image = image.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        nav.tabBarItem.selectedImage = selImage.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        nav.tabBarItem?.imageInsets = UIEdgeInsetsMake(6, -2, -6, 2)
        return nav;
    }

到这儿就完成了Nav类里面的操作

2、TabbarViewcontroller.swift中(继承自UITabBarController,并遵守UITabBarControllerDelegate协议)
  • 在TabbarViewcontroller中写入如下代码:
    定义一个记录选中下标的变量
var selectIndexLast : Int?

构造tabbarVc

self.delegate = self
        self.navigationItem.backBarButtonItem = UIBarButtonItem.init(title: "返回", style: .plain, target: nil, action: nil)
        
        self.tabBar.clipsToBounds = true
        self.tabBar.backgroundImage = UIImage.init(named: "se_base_tabbar_bg")//改成你图片的名字
//        新建Vc
//        let home = HomeViewController()
//        调用函数构造方法
//        let navhome = NavigationViewController().navigationControllerWithRootViewController(Vc: home, image: UIImage(named: "se_base_tabbar_home_gray")!, selImage:  UIImage(named: "se_base_tabbar_home_light")!)
//        
//        let maillist = MailListViewController()
//        let navmaillist = NavigationViewController().navigationControllerWithRootViewController(Vc: maillist, image: UIImage(named: "se_base_tabbar_maillist_gray")!, selImage:  UIImage(named: "se_base_tabbar_maillist_light")!)
//        
//        let helpother = HelpOtherViewController()
//        let navhelpother = NavigationViewController().navigationControllerWithRootViewController(Vc: helpother, image: UIImage(named: "se_base_tabbar_helpother_gray")!, selImage:  UIImage(named: "se_base_tabbar_helpother_light")!)
//        
//        let my = MyViewController()
//        let navmy = NavigationViewController().navigationControllerWithRootViewController(Vc: my, image: UIImage(named: "se_base_tabbar_my_gray")!, selImage:  UIImage(named: "se_base_tabbar_my_light")!)
//        添加到tabbarVc中
//        self.viewControllers = [navhome, navmaillist, navhelpother, navmy]
//        设置默认选中为第一个
        self.tabBarController?.selectedIndex = 0

实现代理方法

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        selectIndexLast = self.selectedIndex
    }
2、AppDelegate
  • 最后在AppDelegate中将根视图设置为TabbarVc就可以了

注:所有用到的tabbarButton上的自定制样式让UI 给图的时候字+图一块切过来,这样就非常省事了。

显示全文