Swift开发之 使用系统的TabbarController

前端之家收集整理的这篇文章主要介绍了Swift开发之 使用系统的TabbarController前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用系统的tabbar解决图片的问题.
用sb使用系统的tabbar,弄好后发现图片始终是蓝色的,并不是原来的图片,后经上网查资料,问问同事,有了下面的这些代码,做个标记.
AppDelegate中
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
initTabBarSelectedItem()
return true
}
func initTabBarSelectedItem()
{
var rootController = self.window?.rootViewController as UITabBarController
var tabBar = rootController.tabBar

var homeItem = tabBar.items?[0] as UITabBarItem
    var selectedImg = UIImage(named: "首页")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    if homeItem.tag == 100
    {
        homeItem.selectedImage = selectedImg
        homeItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.redColor(),NSFontAttributeName:UIFont.systemFontOfSize(14)],forState: UIControlState.Normal)
    }
    var orderItem = tabBar.items?[1] as UITabBarItem
    var orderItemImg = UIImage(named: "订单")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    if orderItem.tag == 101
    {
        orderItem.selectedImage = orderItemImg
        orderItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.redColor()],forState: UIControlState.Selected)
    }

    var releaseItem = tabBar.items?[2] as UITabBarItem
    var releaseItemImg = UIImage(named: "发布")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    if releaseItem.tag == 102
    {
        releaseItem.selectedImage = releaseItemImg
        releaseItem.image = releaseItemImg
    releaseItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.redColor()],forState: UIControlState.Selected)
    }
    var myItem = tabBar.items?[3] as UITabBarItem
    var myItemImg = UIImage(named: "我的")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    if myItem.tag == 103
    {
        myItem.selectedImage = myItemImg
        myItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.redColor()],forState: UIControlState.Selected)
    }

}
原文链接:https://www.f2er.com/swift/326613.html

猜你在找的Swift相关文章