AppDelegate.swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let tabBarController = MianTabBarController()
window?.rootViewController = tabBarController
return true
}
MianTabBarController.swift
import UIKit
class MianTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.selectedIndex = 0
setupItem()
}
private func setupItem (){
let firstVC = ViewController( )
firstVC.title = "1"
setupChildVC(vc: firstVC, title: "首頁", image: "tab-bar_icon_empty-main24", selectedImage: "tab bar_icon_main24")
let secondVC = UIViewController()
secondVC.title = "2"
secondVC.view.backgroundColor = .green
setupChildVC(vc: secondVC, title: "消息", image: "tab-bar_icon_empty-message24", selectedImage: "tab-bar_icon_message24")
let forthVC = UIViewController()
forthVC.title = "3"
forthVC.view.backgroundColor = .brown
setupChildVC(vc: forthVC, title: "我的", image: "tab-bar_icon_mine24", selectedImage: "tab-bar_icon_main24")
}
private func setupChildVC(vc:UIViewController,title:String,image:String,selectedImage:String){
let nav = UINavigationController(rootViewController: vc)
addChildViewController(nav)
nav .tabBarItem.title = title;
nav.tabBarItem.image = UIImage(named: image)
nav.tabBarItem.selectedImage = UIImage(named: selectedImage)
}
}