Swift纯代码封装常用TabBar基本框架

作者: 罗小耳 | 来源:发表于2016-04-06 17:05 被阅读905次

最近在自学Swift,想要写一个纯代码封装常用TabBar基本框架,可是网上查了好多资料发现大家都是跟Xib结合使用的,本人对Xib不是特别擅长,所以想实现一款纯代码封装常用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: "社区")
        
        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.
        
        //设置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版“下厨房”开源项目,有兴趣的可以到GitHub查看,本篇文章源代码也在这里

相关文章

网友评论

    本文标题:Swift纯代码封装常用TabBar基本框架

    本文链接:https://www.haomeiwen.com/subject/eqrmlttx.html