最近在自學(xué)Swift,想要寫一個(gè)純代碼封裝常用TabBar基本框架亿柑,可是網(wǎng)上查了好多資料發(fā)現(xiàn)大家都是跟Xib結(jié)合使用的钦听,本人對(duì)Xib不是特別擅長,所以想實(shí)現(xiàn)一款純代碼封裝常用TabBar基本框架怕膛。
不說廢話,上代碼
//
// TabeBarViewController.swift
// XiaChuFang
//
// Created by 羅金 on 16/3/16.
// Copyright ? 2016年 EasyFlower. All rights reserved.
//
import UIKit
class TabeBarViewController: UITabBarController {
var tabBarBgImg:UIImageView?
var tabBarBgImgSelected:UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
/*
* 更改tabBar選中按鈕的顏色
*/
// self.tabBar.tintColor = UIColor.redColor()
self.setUpAllChildViewController()
}
func setUpAllChildViewController() {
self.setUpOneChildViewController(MainViewController(), image: UIImage.init(named: "main_off")!, selectedImage: UIImage.init(named: "main_on")!, title: "下廚房")
self.setUpOneChildViewController(MarketViewController(), image: UIImage.init(named: "market_off")!, selectedImage: UIImage.init(named: "market_on")!, title: "市集")
self.setUpOneChildViewController(CommunityVC(), image: UIImage.init(named: "community_off")!, selectedImage: UIImage.init(named: "community_on")!, title: "社區(qū)")
self.setUpOneChildViewController(MeViewController(), image: UIImage.init(named: "me_off")!, selectedImage: UIImage.init(named: "me_on")!, title: "我")
}
func setUpOneChildViewController(viewController: UIViewController, image: UIImage, selectedImage: UIImage, title: NSString) {
let navVC = UINavigationController.init(rootViewController: viewController)
// 讓圖片顯示圖片原始顏色 “UIImage” 后+ “.imageWithRenderingMode(.AlwaysOriginal)”
navVC.tabBarItem = UITabBarItem.init(title: title as String, image: image, selectedImage: selectedImage.imageWithRenderingMode(.AlwaysOriginal))
self.addChildViewController(navVC)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Appdelegate里面代碼
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//設(shè)置tabbar
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()
//初始化tabbar
let tabbarVC = TabeBarViewController()
self.window!.rootViewController = tabbarVC
self.window!.makeKeyAndVisible()
return true
}
本人正在不定期更新Swift版“下廚房”開源項(xiàng)目秦踪,有興趣的可以到GitHub查看褐捻,本篇文章源代碼也在這里