朋也的博客 » 首页 » 文章

swift4 UITabBarController 简单使用

作者:朋也
日期:2018-06-29
类别:swift学习笔记(纯代码) 


版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证

直接上图

新建三个ViewController

HomeViewController.swift

import UIKit

class HomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .blue
        self.tabBarController?.title = "主页"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

OrderViewController.swift

import UIKit

class OrderViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .red
        self.tabBarController?.title = "订单"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

MyViewController.swift

import UIKit

class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .yellow
        self.tabBarController?.title = "我"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

创建TabBar布局类

import UIKit

class LayoutViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        let homeNav = HomeViewController()
        let orderNav = OrderViewController()
        let myNav = MyViewController()

        homeNav.tabBarItem.title = "主页"
        orderNav.tabBarItem.title = "订单"
        myNav.tabBarItem.title = "我"

        homeNav.tabBarItem.image = UIImage(named: "home")
        orderNav.tabBarItem.image = UIImage(named: "order")
        myNav.tabBarItem.image = UIImage(named: "my")

        self.viewControllers = [homeNav, orderNav, myNav]

        // 文字图片颜色一块修改
        self.tabBar.tintColor = UIColor.blue
    }

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        self.title = item.title
    }

}

修改AppDelegate类

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    self.window?.rootViewController = UINavigationController(rootViewController: LayoutViewController())

    return true
}

上面用到的图片可以在 https://material.io/tools/icons/?style=baseline 上下载