美文网首页
Swift学习 - UITabBarController使用

Swift学习 - UITabBarController使用

作者: 纵昂 | 来源:发表于2021-03-11 11:11 被阅读0次

    一、在AppDelegate.swift

    //
    //  AppDelegate.swift
    //  FirstTrain
    //
    //  Created by 纵昂 on 2021/3/10.
    //
    
    import UIKit
    
    @main
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            
            window = UIWindow(frame: UIScreen.main.bounds)
            window?.backgroundColor = .white
    //     我们修改 AppDelegate.swift,让 window 的 rootViewController 为我们的 MainTabBarController:
            window?.rootViewController = MainTabBarController()
    //        window?.rootViewController = UINavigationController(rootViewController: MainTabBarController()) //添加导航
            window?.makeKeyAndVisible()
            
            return true
        }
    }
    
    二、去创建五个试图控制器 试图控制器.png

    紧接着创建UITabBarController,开始布局

    //
    //  MainTabBarController.swift
    //  FirstTrain
    //
    //  Created by 纵昂 on 2021/3/10.
    //
    
    import UIKit
    
    class MainTabBarController: UITabBarController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            initTabBar()
        }
        
        func initTabBar() {
            let home = UINavigationController(rootViewController: HomeViewController())
            home.tabBarItem.title = "首页"
            home.tabBarItem.image = UIImage(named: "tabbar_car_select.png")
            
            let category = UINavigationController(rootViewController: HHHViewController())
            category.tabBarItem.title = "分类"
            category.tabBarItem.image = UIImage(named: "tabbar_fenlei_select.png")
           
            let cart = UINavigationController(rootViewController: FirstViewController())
            cart.tabBarItem.title = "购物车"
            cart.tabBarItem.image = UIImage(named: "tabbar_car_select.png")
    
            let mine = UINavigationController(rootViewController: MYViewController())
            mine.tabBarItem.title = "我的"
            mine.tabBarItem.image = UIImage(named: "tabbar_serach_select.png")
    
            let message = UINavigationController(rootViewController: MessageViewController())
            message.tabBarItem.title = "消息"
            message.tabBarItem.image = UIImage(named: "tabbar_message_select")
            
            
            viewControllers = [home,category,cart,mine]
            
            // 设置 tabBar & tabBarItem
            setTabBarItemAttributes(bgColor: UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1))
            
        }
        
        /// 这种方式比较灵活
           func setTabBarItemAttributes(fontName: String = "Courier",
                                        fontSize: CGFloat = 14,
                                        normalColor: UIColor = .gray,
                                        selectedColor: UIColor = .red,
                                        bgColor: UIColor = .white) {
               // tabBarItem 文字大小
               var attributes: [NSAttributedString.Key: Any] = [.font: UIFont(name: fontName, size: fontSize)!]
               
               // tabBarItem 文字默认颜色
               attributes[.foregroundColor] = normalColor
               UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .normal)
               
               // tabBarItem 文字选中颜色
               attributes[.foregroundColor] = selectedColor
               UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .selected)
               
               // tabBar 文字、图片 统一选中高亮色
               tabBar.tintColor = selectedColor
               
               // tabBar 背景色
               tabBar.barTintColor = bgColor
           }
    
    
        /*
        // 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.
        }
        */
    
    }
    
    

    三、首页代码

    //
    //  HomeViewController.swift
    //  FirstTrain
    //
    //  Created by 纵昂 on 2021/3/10.
    //
    
    import UIKit
    
    class HomeViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            title = "Home" //导航标题
            
            
        }
        
    
        /*
        // 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.
        }
        */
    
    }
    

    常用的tabBar底部控制器导航,此文不厚严谨,后续不定时更新内容干货
    代码已上传到GitHuB上

    相关文章

      网友评论

          本文标题:Swift学习 - UITabBarController使用

          本文链接:https://www.haomeiwen.com/subject/xgpnqltx.html