ios tabbar 踩坑记

2019-07-22  本文已影响0人  guoheng1996

设置tabbar背景为透明,我按照网上的设置backgroundcolor没有达到我的效果
于是设置图片:

继承类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)

设置图片原本的颜色

自定义的UITabBarController

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

上一篇下一篇

猜你喜欢

热点阅读