效果圖如下:
1.首先創(chuàng)建兩個(gè)ViewController
HomePageViewController:
<pre>
class HomePageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.purple
}
}
</pre>
SecondViewController:
<pre>
class HomePageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
</pre>
2.創(chuàng)建一個(gè)RootTabBarViewController繼承自UITabBarController ,在RootTabBarController類里創(chuàng)建tabbar的子控制器
<pre>
class RootTabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
//創(chuàng)建tabbar的子控制器
self.creatSubViewControllers()
}
func creatSubViewControllers(){
let firstVC = HomePageViewController()
firstVC.title = "首頁(yè)"
let navi0 = UINavigationController(rootViewController:firstVC)
navi0.tabBarItem.title = "首頁(yè)"
navi0.tabBarItem.image = UIImage (named:"sy")
navi0.tabBarItem.selectedImage = UIImage(named:"sy1")
let secondVC = SecondViewController()
secondVC.title = "我的"
let navi1 = UINavigationController(rootViewController:secondVC)
navi1.tabBarItem.title = "我的"
navi1.tabBarItem.image = UIImage (named:"wd")
navi1.tabBarItem.selectedImage = UIImage(named:"wd1")
tabBar.tintColor = UIColor.orange
let tabArray = [navi0,navi1]
self.viewControllers = tabArray
}
</pre>
3.在AppDelegate類里指定RootTabBarController為根視圖
<pre>
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = UIColor.white
self.window?.makeKeyAndVisible()
let root = RootTabBarViewController()
self.window?.rootViewController=root
return true
}
</pre>