zl程序教程

您现在的位置是:首页 >  其他

当前栏目

swift5 搭建 tabBar (1) 完整教程【下期在这个基础上中间弄个+号】

教程基础 搭建 完整 这个 中间 tabBar swift5
2023-09-27 14:27:38 时间

效果 

 

前期需要的配置

https://blog.csdn.net/mp624183768/article/details/108935803

继续配置 为了让背景变成白色 弄一个全局的基类

//
//  BaseViewController.swift
//  News
//
//  Created by liuan on 2020/10/6.
//

import UIKit

class BaseViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        // Do any additional setup after loading the view.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

主界面

//
//  MyTabBarController.swift
//  News
//
//  Created by liuan on 2020/10/5.
//

import UIKit

class MyTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let tabbar = UITabBar.appearance()
        tabbar.tintColor = UIColor(red: 245 / 255.0, green: 90 / 255.0, blue: 93 / 255.0, alpha: 1.0)
//
        //添加子控制器
addChildViewControllers()
       
    }
    ///添加子控制器
    func addChildViewControllers(){
        setChildViewController(HomeViewController(), title: "首页", imageName: "home_tabbar_32x32_", selectedImageName: "home_tabbar_press_32x32_")
        

            setChildViewController(VideoViewController(), title: "视频", imageName: "video_tabbar_32x32_", selectedImageName: "video_tabbar_press_32x32_")


            setChildViewController(HuoshanViewController(), title: "小视频", imageName: "huoshan_tabbar_32x32_", selectedImageName: "huoshan_tabbar_press_32x32_")


            setChildViewController(MineViewController(), title: "我的", imageName: "mine_tabbar_32x32_", selectedImageName: "mine_tabbar_press_32x32_")
    }
    ///初始化主控制器
     func setChildViewController(_ childController: UIViewController,title:String,imageName:String,selectedImageName:String) {
        //设置tabbar 文字图片
        
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: selectedImageName)
        childController.tabBarItem.title = title
        
        //添加导航控制为 TabController 的子控制器
        let navVc = MyNavigationController(rootViewController: childController)
        addChild(navVc)
            
    }
   

}

其他的四个内容都是一样的空的 我就贴一个



import UIKit

class MineViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.white
    }

}

至于资源文件和完整教程大家如果感兴趣可以到

https://www.yzdzy.com/detail.php?id=11526

这里5元购买下完整教程