微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift UITabBarItem修改字体颜色和图标颜色

storyboard创建的选项卡,图片颜色固定,字体颜色固定。可以说完全没法用,查阅了好久的api和资料才找到修改的办法。


将UITabBarItem连线到FirstViewController

代码如下:

//
//  FirstViewController.swift
//  demo
//
//  Created by sunger on 15/3/24.
//  copyright (c) 2015年 sunger. All rights reserved.
//

import UIKit

class FirstViewController: UIViewController {
    @IBOutlet weak var tab1: UITabBarItem!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.
        //这里修改图片颜色,使得图片和原图颜色一致
        tab1.image=UIImage(named: "tab1")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
        //这里修改字体颜色
        var attributes =  [NSForegroundColorAttributeName: UIColor(red: 220.0/255.0,green: 104.0/255.0,blue: 1.0/255.0,alpha: 1.0),NSFontAttributeName: UIFont(name: "Heiti SC",size: 24.0)!]
        tab1.setTitleTextAttributes(attributes,forState: UIControlState.Selected)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // dispose of any resources that can be recreated.
    }


}

主要参考官方api和如下几篇文章

1.修改图片颜色

2.修改字体颜色

http://www.cnblogs.com/xgbzsc/archive/2013/01/16/2862611.html

http://blog.csdn.net/willyang519/article/details/38855719

http://stackoverflow.com/questions/7810563/how-do-you-use-settitletextattributesforstate-in-uibaritem


最后实现如下效果

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐