UITabBarController
UITabBarController实现底部切换的效果非常方便,如下图
下面我们学习如何实现这种效果
1. UITabBarController的创建
AppDelegate.swift中,我们创建一个UITabBarController
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.navBarConfig()
let tabBarController = MainTabBarController()
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
return true
}
上面的代码中我们创建了一个MainTabBarController,并且它设置为rootViewController, 下面我们实现MainTabBarController类
import UIKit
class MainTabBarController: UITabBarController {
let titles = ["美女", "星座", "笑话"]
override func viewDidLoad() {
super.viewDidLoad()
let vc1 = ViewController()
let nav1 = UINavigationController(rootViewController: vc1)
// 设置底部标签的标题
vc1.tabBarItem.title = titles[0]
// 设置底部标签的图片
vc1.tabBarItem.image = UIImage(named: "fa-female")
let vc2 = ViewController2()
let nav2 = UINavigationController(rootViewController: vc2)
vc2.tabBarItem.title = titles[1]
vc2.tabBarItem.image = UIImage(named: "fa-star")
let vc3 = ViewController3()
let nav3 = UINavigationController(rootViewController: vc3)
vc3.tabBarItem.title = titles[2]
vc3.tabBarItem.image = UIImage(named: "fa-plane")
self.viewControllers = [nav1, nav2, nav3]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
上面的代码中我们设置了UITabBarController的3个内容页, 并设置了底部标签的标题和图片, 其中ViewController和ViewController2使用第5节UINavigationController的代码.ViewController3中简单的修改一下背景颜色即可,方便区别其它UIViewController.
import UIKit
class ViewController3: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.blueColor()
self.title = "笑话"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
现在我们看看效果:
2. 设置底部tab栏的颜色,标题和图片选中颜色
在AppDelegate.swift中我们添加一个方法:
func tabBarConfig() {
let tabBar = UITabBar.appearance()
// 设置按钮和文字的选中颜色
tabBar.tintColor = UIColor(red: 0xff/255.0, green: 0x52/255.0, blue: 0x56/255.0, alpha: 1.0)
// 设置底部的tabBar的颜色
tabBar.barTintColor = UIColor.whiteColor()
// 底部的tabBar是否半透明
tabBar.translucent = false
}
然后调用它
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.tabBarConfig()
self.navBarConfig()
let tabBarController = MainTabBarController()
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
return true
}
运行程序, 发现颜色已经变成我们需要的颜色了
3. 使用storyboard
操作的东西用语言描述不清楚,最好找个视频看看storyboard的使用,也不难!
运行程序:
4. 完整代码
AppDelegate.swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.tabBarConfig()
self.navBarConfig()
let tabBarController = MainTabBarController()
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
return true
}
func navBarConfig() {
let bar = UINavigationBar.appearance()
//设置导航栏的颜色
bar.barTintColor = UIColor(red: 0xff/255.0, green: 0x52/255.0, blue: 0x56/255.0, alpha: 1.0)
//设置按钮的颜色
bar.tintColor = UIColor.whiteColor()
// 设置标题颜色
bar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()];
// 导航栏是否半透明
bar.translucent = false
}
func tabBarConfig() {
let tabBar = UITabBar.appearance()
// 设置按钮和文字的选中颜色
tabBar.tintColor = UIColor(red: 0xff/255.0, green: 0x52/255.0, blue: 0x56/255.0, alpha: 1.0)
// 设置底部的tabBar的颜色
tabBar.barTintColor = UIColor.whiteColor()
// 底部的tabBar是否半透明
tabBar.translucent = false
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
self.title = "美女"
let button = UIButton(type: .Custom)
button.frame = CGRect(x: 0, y: 100, width: 100, height: 40)
// 设置不同状态文字
button.setTitle("Normal", forState: .Normal)
button.setTitle("Highlighted", forState: .Highlighted)
button.setTitle("Disabled", forState: .Disabled)
// 设置不同状态文字颜色
button.setTitleColor(UIColor.redColor(), forState: .Normal)
button.setTitleColor(UIColor.blueColor(), forState: .Highlighted)
button.setTitleColor(UIColor.grayColor(), forState: .Disabled)
button.setBackgroundImage(UIImage(named: "normal"), forState: .Normal)
button.setBackgroundImage(UIImage(named: "highlighted"), forState: .Highlighted)
button.setBackgroundImage(UIImage(named: "disabled"), forState: .Disabled)
button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
self.view.addSubview(button)
}
func buttonAction(sender: UIButton) {
// 跳转到ViewController2
let vc = ViewController2()
vc.age = 100
self.navigationController?.pushViewController(vc, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
UIViewController2.swift
import UIKit
class ViewController2: UIViewController {
var age: Int?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 0xff/255.0, green: 0x00/255.0, blue: 0xff/255.0, alpha: 1.0)
self.title = "星座"
let button = UIButton(type: .Custom)
button.frame = CGRect(x: 0, y: 200, width: 100, height: 40)
// 设置不同状态文字
button.setTitle("返回", forState: .Normal)
button.setTitle("返回", forState: .Highlighted)
button.setTitle("返回", forState: .Disabled)
// 设置不同状态文字颜色
button.setTitleColor(UIColor.redColor(), forState: .Normal)
button.setTitleColor(UIColor.blueColor(), forState: .Highlighted)
button.setTitleColor(UIColor.grayColor(), forState: .Disabled)
button.setBackgroundImage(UIImage(named: "normal"), forState: .Normal)
button.setBackgroundImage(UIImage(named: "highlighted"), forState: .Highlighted)
button.setBackgroundImage(UIImage(named: "disabled"), forState: .Disabled)
button.addTarget(self, action: "backAction:", forControlEvents: .TouchUpInside)
self.view.addSubview(button)
// 添加导航栏详情按钮
let detailBtn = UIBarButtonItem(title: "详情", style: .Plain, target: self, action: "detailAction:")
self.navigationItem.rightBarButtonItem = detailBtn
}
func detailAction(sender: UIBarButtonItem) {
print("跳转到详情\(sender.title)")
}
func backAction(sender: UIButton) {
// 返回
self.navigationController?.popViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
ViewController3.swift
import UIKit
class ViewController3: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.blueColor()
self.title = "笑话"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
MainTabBarController.swift
import UIKit
class MainTabBarController: UITabBarController {
let titles = ["美女", "星座", "笑话"]
override func viewDidLoad() {
super.viewDidLoad()
let vc1 = ViewController()
let nav1 = UINavigationController(rootViewController: vc1)
// 设置底部标签的标题
vc1.tabBarItem.title = titles[0]
// 设置底部标签的图片
vc1.tabBarItem.image = UIImage(named: "fa-female")
let vc2 = ViewController2()
let nav2 = UINavigationController(rootViewController: vc2)
vc2.tabBarItem.title = titles[1]
vc2.tabBarItem.image = UIImage(named: "fa-star")
let vc3 = ViewController3()
let nav3 = UINavigationController(rootViewController: vc3)
vc3.tabBarItem.title = titles[2]
vc3.tabBarItem.image = UIImage(named: "fa-plane")
self.viewControllers = [nav1, nav2, nav3]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}