您的位置:首页 > 移动开发 > Swift

swift TabBar

2016-05-07 17:57 344 查看
先上效果图如下:



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
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: