【问题标题】:Swift TabBar Item color and background colorSwift TabBar 项目颜色和背景颜色
【发布时间】:2015-08-12 14:31:34
【问题描述】:

我正在尝试在 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
    }
}

【问题讨论】:

    标签: swift ios8 uitabbarcontroller


    【解决方案1】:

    可能有点晚了,但要创建这个“相机项目”,我认为唯一的解决方案是创建一个 UIButton 并将按钮的中心设置为 UITabBar 的中心。你可以在这个post 中看到如何做到这一点。之后,您可以将图像设置为具有此背景颜色的按钮。

    更新:

    我用 swift 创建了这个sample project,您可以在其中看到如何在 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")
    
        }
    
    }
    

    【讨论】:

    • 谢谢@angeldev,我会看看这个,如果它解决了问题,请告诉你。
    • @ggomersall 我刚刚在我的一个项目中尝试过它,它对我有用。我也很快做到了。如果您有任何问题,请告诉我:)。
    • 太棒了!!我会告诉你的
    猜你喜欢
    • 2020-12-26
    • 2015-04-05
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多