先上效果图如下:
1. 在APP 的AppDelegate页面中加载TFTabBarViewController类
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { self.window = UIWindow(frame:UIScreen.mainScreen().bounds) //let rootController = RootViewController(style: UITableViewStyle.Plain) let rootController = TFTabBarViewController() //TFLoginViewController() let rootNav = UINavigationController(rootViewController: rootController) self.window!.rootViewController = rootNav self.window!.backgroundColor = UIColor.whiteColor() self.window!.makeKeyAndVisible() return true }
2. 创建 FirstViewController ,SecondViewController,ThirdViewController,FourthViewController四个页面,然后设置下页面的颜色(便于点击下面的TabBar时,可以看到切换的效果)
3. 将刚刚创建的页面加载到TabBar中去
以下是源码:
import UIKit class TFTabBarViewController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() self.loadTabBarViewControllers() } func loadTabBarViewControllers(){ let firstVC = FirstViewController () let item1 : UITabBarItem = UITabBarItem (title: "首页",image: UIImage(named: "home_normal"),selectedImage: UIImage(named: "home_highlight")) firstVC.tabBarItem = item1 let secondVC = SecondViewController () let item2 : UITabBarItem = UITabBarItem (title: "购物",image: UIImage(named: "message_normal"),selectedImage: UIImage(named: "message_highlight")) secondVC.tabBarItem = item2 let thirdVC = ThirdViewController () let item3 : UITabBarItem = UITabBarItem (title: "旅游",image: UIImage(named: "mycity_normal"),selectedImage: UIImage(named: "mycity_highlight")) thirdVC.tabBarItem = item3 let fourthVC = FourthViewController () let item4 : UITabBarItem = UITabBarItem (title: "旅游",image: UIImage(named: "account_normal"),selectedImage: UIImage(named: "account_highlight")) fourthVC.tabBarItem = item4 let tabArray = [firstVC,secondVC,thirdVC,fourthVC] self.viewControllers = tabArray } }原文链接:https://www.f2er.com/swift/323873.html