ios tabbar 踩坑記

設(shè)置tabbar背景為透明,我按照網(wǎng)上的設(shè)置backgroundcolor沒有達(dá)到我的效果
于是設(shè)置圖片:

繼承類UITabBarController的viewDidLoad() 中

        self.tabBar.backgroundImage = imageWithColor(color: UIColor(red: 0, green: 0, blue: 0, alpha: 0.2))

在底下寫這個方法
    func imageWithColor(color: UIColor) -> UIImage? {
        let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(color.cgColor)
        context?.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }

自定義文字選擇和未選中顏色:

自定義的UITabBarController 的viewDidLoad() 中
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor(red: 156/255.0, green: 179/255.0, blue: 184/255.0, alpha: 1.0)], for: .normal)
        
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)

設(shè)置圖片原本的顏色

自定義的UITabBarController

    override func awakeFromNib() {
        self.tabBar.items?.forEach({ (it) in
            it.image = it.image?.withRenderingMode(.alwaysOriginal)
            it.selectedImage = it.selectedImage?.withRenderingMode(.alwaysOriginal)
        })
    }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容