Swift实现自定义的UITabBarController

效果图:

Swift实现自定义的UITabBarController_第1张图片

首先创建三个TabVIewController:

------------FirstTabViewController------------------

    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.title = "TabBarItem #1"
        
        self.view.backgroundColor = UIColor.purpleColor()
    }
------------SecondTabViewController--------------

    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.title = "TabBarItem#2"
        
        self.view.backgroundColor = UIColor.yellowColor()
    }

---------ThirdTabViewController-------------(无需添加UINavigationBar)

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.blueColor()
    }

-------------UITabViewController---------继承自UITabBarController

<pre name="code" class="html">//
//  TabBarViewController.swift
//

import UIKit

class TabBarViewController: UITabBarController
{

    var screenWidth: CGFloat?
    var screenHeight: CGFloat?
    
    let tabBarWidth: CGFloat = 64
    let tabBarHeight: CGFloat = 49
    let tabBarViewHeight: CGFloat = 60
    
    var tabButtons = [UIButton]()
    var imgArr = ["TabBar1", "TabBar2" ,"TabBar3"];
    var imgSelArr = ["TabBar1Sel", "TabBar2Sel", "TabBar3Sel"]
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        var screenFrame = UIScreen.mainScreen().bounds
        screenWidth = screenFrame.width
        screenHeight = screenFrame.height
        
        println(screenWidth)
        
        self.view.backgroundColor = UIColor.whiteColor()
        
        self.tabBar.hidden = true
        
        initControllers()
        
        customTabBar()
        
    }
    
    func customTabBar() -> Void
    {
        let tabBarOffsetX = screenWidth!/3
        let tabBarX = tabBarOffsetX/2 - tabBarWidth/2
        let tabBarY = tabBarViewHeight/2 - tabBarHeight/2
        var tabBarView = UIView(frame: CGRectMake(0, screenHeight! - tabBarViewHeight, screenWidth!, tabBarViewHeight))
        tabBarView.backgroundColor = UIColor.blackColor()
        self.view.addSubview(tabBarView)
        
        for index in 0..<imgArr.count
        {
            var tabBar_X = (CGFloat)(index) * tabBarOffsetX
            var btn = UIButton(frame: CGRectMake((CGFloat)(tabBarX + tabBar_X), (CGFloat)(tabBarY), tabBarWidth, tabBarHeight))
            if(index == 0)
            {
                btn.setBackgroundImage(UIImage(named: imgSelArr[index]), forState: UIControlState.Normal)
            }else{
                btn.setBackgroundImage(UIImage(named: imgArr[index]), forState: UIControlState.Normal)
            }
            
            btn.tag = index + 100
            btn.addTarget(self, action: "tabAction:", forControlEvents: UIControlEvents.TouchUpInside)
            
            tabBarView.addSubview(btn)
            tabButtons.append(btn)
        }

    }
    
    func tabAction(obj: UIButton) -> Void
    {
        var indexSel = obj.tag - 100
        self.selectedIndex = indexSel
        for index in 0..<tabButtons.count
        {
            if(index == indexSel)
            {
                tabButtons[indexSel].setBackgroundImage(UIImage(named: imgSelArr[indexSel]), forState: UIControlState.Normal)
            }else{
                tabButtons[index].setBackgroundImage(UIImage(named: imgArr[index]), forState: UIControlState.Normal)
            }
        }
    }
    
    func initControllers() -> Void
    {
        let firstTabView = FirstTabViewController()
        let secondTabView = SecondTabViewController()
        let thirdTabView = ThirdTabViewController()
        
        var viewArr = [firstTabView, secondTabView, thirdTabView]
        var viewCtlArr = [AnyObject]()
        for index in 0..<viewArr.count
        {
            if(index != 2)
            {
                var navController = UINavigationController(rootViewController: viewArr[index])
                viewCtlArr.append(navController)
            }else{
                viewCtlArr.append(viewArr[index])
            }
        }
        
        self.viewControllers = viewCtlArr
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }
    
}


 
 

最后,在AppDelegate.swift中设置根视图:

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
    { 
        var tabBarController = TabBarViewController()
        
        self.window!.rootViewController = tabBarController
        
        return true
    }


你可能感兴趣的:(Swift实现自定义的UITabBarController)