1.效果图
2.NewsViewController.swift
-
- import UIKit
-
- class NewsViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- self.view.backgroundColor=UIColor.blueColor()
- self.title="新闻"
- }
- }
3.MoviewViewController.swift
-
- import UIKit
-
- class MovieViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- self.view.backgroundColor=UIColor.redColor()
- self.title="电影"
- }
- }
4.AppDelegate.swift
-
- import UIKit
-
- @UIApplicationMain
- class AppDelegate: UIResponder, UIApplicationDelegate {
-
- var window: UIWindow?
-
-
- func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
- self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
-
- self.window!.backgroundColor = UIColor.whiteColor()
- self.window!.makeKeyAndVisible()
-
- var root=RootViewController()
- self.window!.rootViewController=root
- return true
- }
-
- func applicationWillResignActive(application: UIApplication) {
-
-
- }
-
- func applicationDidEnterBackground(application: UIApplication) {
-
-
- }
-
- func applicationWillEnterForeground(application: UIApplication) {
-
- }
-
- func applicationDidBecomeActive(application: UIApplication) {
-
- }
-
- func applicationWillTerminate(application: UIApplication) {
-
- }
-
-
- }
5.RootViewController.swift
-
- import UIKit
-
- class RootViewController: UITabBarController {
-
- var tabBarBgImg:UIImageView?
- var tabBarBgImgSelected:UIImageView?
-
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- self.tabBar.hidden=true
- customTabBar()
- loadViewController()
- }
-
- func test(tap:UITapGestureRecognizer){
- var view=tap.view
- var index=view.tag as Int
- var z=(index)*57
- var c=CGFloat(z)
- var x:CGFloat=5.0 + c
- var y=tabBarBgImg!.frame.size.height/2-45/2
- UIView.beginAnimations("test",context:nil)
- tabBarBgImgSelected!.frame = CGRectMake(x ,y, 50, 45)
- UIView.commitAnimations()
-
- self.selectedIndex=view.tag
-
- }
-
-
-
- func customTabBar(){
-
- var height=UIScreen.mainScreen().bounds.size.height
- var width=UIScreen.mainScreen().bounds.size.width
- var tabW=width
- var tabH=height-49
- tabBarBgImg=UIImageView(frame:CGRectMake(0,tabH,tabW,49))
-
- tabBarBgImg!.userInteractionEnabled=true
- tabBarBgImg!.image=UIImage(named:"tab_bg_all")
-
-
- var y=tabBarBgImg!.frame.size.height/2-45/2
- tabBarBgImgSelected=UIImageView(frame:CGRectMake(5,y, 50, 45))
- tabBarBgImgSelected!.image=UIImage(named:"selectTabbar_bg_all1")
- tabBarBgImg!.addSubview(tabBarBgImgSelected)
-
- var x:CGFloat=0
- var images=["icon_cinema","msg_new"]
- var titles=["电影","新闻"]
- var titleFont=UIFont.systemFontOfSize(12)
- for index in 0..2{
- var imgView=UIImageView(frame:CGRectMake( x+18, y+5, 22, 22))
-
- imgView.userInteractionEnabled=true
- imgView.tag=index
- var tap=UITapGestureRecognizer(target:self,action:Selector("test:"))
- imgView.addGestureRecognizer(tap)
-
- imgView.image = UIImage(named:images[index])
- tabBarBgImg!.addSubview(imgView)
- var title=UILabel(frame:CGRectMake(x+16,y+26,45,15))
- title.text=titles[index]
- title.font=titleFont
- title.textColor = UIColor.whiteColor()
- tabBarBgImg!.addSubview(title)
- x+=57
- }
- self.view.addSubview(tabBarBgImg)
-
- }
-
-
- func loadViewController(){
-
- var movie=MovieViewController()
- var movieItem=UITabBarItem(tabBarSystemItem: .Favorites,tag:1)
- movie.tabBarItem=movieItem
- var movieNav=UINavigationController(rootViewController:movie)
-
- var news=NewsViewController()
- var newsItem=UITabBarItem(tabBarSystemItem: .Favorites,tag:2)
- news.tabBarItem=newsItem
- var newsNav=UINavigationController(rootViewController:news)
-
-
- var ctrls=[movieNav,newsNav]
-
- self.setViewControllers(ctrls,animated:true)
- }
-
-
- }
swift 自定义TabBarItem
原文:http://www.cnblogs.com/Free-Thinker/p/4863372.html