美文网首页iOS 开发继续加油
Swift创建UITabBarController 自定义选中i

Swift创建UITabBarController 自定义选中i

作者: 7517a72b50f8 | 来源:发表于2016-08-10 15:01 被阅读672次

    16进制数转UIColor:

    import UIKit
    
    public extension UIColor {
        static func hexStringToColor(hexString: String) -> UIColor {
            
            var mString = hexString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
            
            if mString.characters.count < 6 { return UIColor.blackColor() }
            if mString.hasPrefix("0X") { mString = mString.substringFromIndex(mString.startIndex.advancedBy(2)) }
            if mString.hasPrefix("#") {mString = mString.substringFromIndex(mString.startIndex.advancedBy(1))}
            if mString.characters.count != 6 { return UIColor.blackColor() }
            
            var range: NSRange = NSMakeRange(0, 2)
            
            let rString = (mString as NSString).substringWithRange(range)
            range.location = 2
            let gString = (mString as NSString).substringWithRange(range)
            range.location = 4
            let bString = (mString as NSString).substringWithRange(range)
            
            var r: UInt32 = 0x0
            var g: UInt32 = 0x0
            var b: UInt32 = 0x0
            
            NSScanner.init(string: rString).scanHexInt(&r)
            NSScanner.init(string: gString).scanHexInt(&g)
            NSScanner.init(string: bString).scanHexInt(&b)
            
            return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: CGFloat(1.0))
        }
    }
    

    继承于UITabBarController

    import UIKit
    
    class MCustomTabBarController: UITabBarController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            creatSubViewControllers()
        }
        
        func creatSubViewControllers() {
            let mainViewController = MMainViewController()
            let mainItem: UITabBarItem = UITabBarItem(title: "首页", image: UIImage(named: "qt_tab_shy_n"), selectedImage: UIImage(named: "qt_tab_shy_f"))
            mainViewController.tabBarItem = mainItem
            
            let houseListViewController = MHouseListViewController()
            let houseListItem: UITabBarItem = UITabBarItem(title: "房源", image: UIImage(named: "qt_tab_fy_n"), selectedImage: UIImage(named: "qt_tab_fy_f"))
            houseListViewController.tabBarItem = houseListItem
            
            let mapViewController = MMapViewController()
            let mapItem: UITabBarItem = UITabBarItem(title: "地图", image: UIImage(named: "qt_tab_dt_n"), selectedImage: UIImage(named: "qt_tab_dt_f"))
            mapViewController.tabBarItem = mapItem
            
            let mineViewController = MMineViewController()
            let mineItem: UITabBarItem = UITabBarItem(title: "我", image: UIImage(named: "qt_tab_wd_n"), selectedImage: UIImage(named: "qt_tab_wd_f"))
            mineViewController.tabBarItem = mineItem
            
            let tabBarArray = [mainViewController, houseListViewController, mapViewController, mineViewController]
            self.viewControllers = tabBarArray
        }
        
    }
    

    在AppDelegate中:

    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.window?.makeKeyAndVisible()
            let root = MCustomTabBarController()
            root.tabBar.tintColor = UIColor.hexStringToColor("#ff8500")
            self.window?.rootViewController = root
            
            return true
        }
    

    效果图:

    Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png

    相关文章

      网友评论

      本文标题:Swift创建UITabBarController 自定义选中i

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