Swift TabBar 项目颜色和背景颜色

2023-12-27

我正在尝试在 TabBarController 中设置 TabBar 上单个项目的图标颜色和背景颜色。

我的 TabBar 上有 5 个图标,其中 4 个我已经设置了颜色,这只是我正在努力解决的最后一个图标。我在图像下面附加了一个链接,以显示我想要实现的目标。

具体的图标是(图像中间的那个)用于我的相机输入,我希望该图标为白色,背景为红色。 到目前为止我的代码是这样的 - 在我的 TabBarController 上:

var imageNames = ["FeedIcon", "ExploreIcon", "CameraIcon", "ActivityIcon", "MyProfileIcon"]

    let tabItems = tabBar.items as! [UITabBarItem]
    for (index, value) in enumerate(tabItems)
    {
       var imageName = imageNames[index]
        value.image = UIImage(named: imageName)
        value.imageInsets = UIEdgeInsetsMake(5.0, 0, -5.0, 0)

    }
    //for below i've used a extension for imageWithColor to set the color of my icons
    for tabItems in self.tabBar.items as! [UITabBarItem] {
        if let image = tabItems.image {
            tabItems.image = image.imageWithColor(UIColor(red: 57.0/255.0, green: 57.0/255.0, blue: 57.0/255.0, alpha: 1.0)).imageWithRenderingMode(.AlwaysOriginal)
        }
    }

我的扩展:

    extension UIImage {
    func imageWithColor(tintColor: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)

        let context = UIGraphicsGetCurrentContext() as CGContextRef
        CGContextTranslateCTM(context, 0, self.size.height)
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextSetBlendMode(context, kCGBlendModeNormal)

        let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
        CGContextClipToMask(context, rect, self.CGImage)
        tintColor.setFill()
        CGContextFillRect(context, rect)

        let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
        UIGraphicsEndImageContext()

        return newImage
    }
}

可能有点晚了,但要创建这个“相机项目”,我认为唯一的解决方案是创建一个 UIButton 并将按钮的中心设置为 UITabBar 的中心。您可以在此处查看如何执行此操作post http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/。之后,您可以将图像设置为具有此背景颜色的按钮。

UPDATE:

我创建了这个示例项目 https://github.com/angeldev/iOSDev/tree/master/buttonOnTabBar用 swift 编写,您可以在其中看到如何在 tabBar 上添加此按钮。

主要问题是子类化 UITabBarController 并从此类添加按钮。

class TabBarViewController: UITabBarController {

    override func viewDidLoad() {

        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    }

    override func viewDidAppear(animated: Bool) {

        // Creates image of the Button
        let imageCameraButton: UIImage! = UIImage(named: "cameraIcon")

        // Creates a Button
        let cameraButton = UIButton(type: .Custom)
        // Sets width and height to the Button
        cameraButton.frame = CGRectMake(0.0, 0.0, imageCameraButton.size.width, imageCameraButton.size.height);
        // Sets image to the Button
        cameraButton.setBackgroundImage(imageCameraButton, forState: .Normal)
        // Sets the center of the Button to the center of the TabBar
        cameraButton.center = self.tabBar.center
        // Sets an action to the Button
        cameraButton.addTarget(self, action: "doSomething", forControlEvents: .TouchUpInside)

        // Adds the Button to the view
        self.view.addSubview(cameraButton)

    }

    func doSomething() {

        print("Action of camera button")

    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swift TabBar 项目颜色和背景颜色 的相关文章

随机推荐