【问题标题】:Set background color of active tab bar item in Swift在 Swift 中设置活动标签栏项目的背景颜色
【发布时间】:2015-03-14 03:34:27
【问题描述】:

如果可能的话,我希望在不使用图像的情况下实现这一点。有没有办法以编程方式创建图像中显示的效果,而不必将每个选项卡呈现为图像?

我在 SO 上查看的每个问题都有保存为 JPG 的选项卡,这比我觉得应该做的工作要多。

有什么想法吗?

【问题讨论】:

    标签: ios swift uitabbar


    【解决方案1】:

    我对@matcartmill 采取了类似的方法,但不需要特殊图像。 此解决方案仅基于您的颜色。

    // set red as selected background color
    let numberOfItems = CGFloat(tabBar.items!.count)
    let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height)
    tabBar.selectionIndicatorImage = UIImage.imageWithColor(color: UIColor.red, size: tabBarItemSize).resizableImage(withCapInsets: UIEdgeInsets.zero)
    
    // remove default border
    tabBar.frame.size.width = self.view.frame.width + 4
    tabBar.frame.origin.x = -2
    

    我正在使用UIImage 的以下扩展名:

    extension UIImage {
    
        class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
            let rect: CGRect = CGRectMake(0, 0, size.width, size.height)
            UIGraphicsBeginImageContextWithOptions(size, false, 0)
            color.setFill()
            UIRectFill(rect)
            let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
    
    }
    

    我希望这会有所帮助!

    为斯威夫特 4

    extension UIImage {
    
       class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
        let rect: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        color.setFill()
        UIRectFill(rect)
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
       }
    }
    

    【讨论】:

    • 太棒了!实际上我只是不得不再次使用这种方法,所以我回到了这篇文章。这是我见过的最好的方法。
    • 它不适用于 iPhone 7s 但适用于 iPhone 5s 为什么?
    • @schickling 这在 iOS 11 /xcode 9 之前一直很好。有什么想法让它像 iOS 11 之前一样适合整个项目吗?
    【解决方案2】:

    更新到 SWIFT 3:

    let numberOfItems = CGFloat((tabBarController?.tabBar.items!.count)!)
    
    let tabBarItemSize = CGSize(width: (tabBarController?.tabBar.frame.width)! / numberOfItems,
                                height: (tabBarController?.tabBar.frame.height)!)
    
    tabBarController?.tabBar.selectionIndicatorImage
        = UIImage.imageWithColor(color: UIColor.black,
                                 size: tabBarItemSize).resizableImage(withCapInsets: .zero)
    
    tabBarController?.tabBar.frame.size.width = self.view.frame.width + 4
    tabBarController?.tabBar.frame.origin.x = -2
    
    extension UIImage
    {
        class func imageWithColor(color: UIColor, size: CGSize) -> UIImage
        {
            let rect: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
            UIGraphicsBeginImageContextWithOptions(size, false, 0)
            color.setFill()
            UIRectFill(rect)
            let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext()
            return image
        }
    }
    

    【讨论】:

    • 是的,它在 ios 11 中不起作用。@ynamao 确实得到了任何解决方案?
    【解决方案3】:

    所以这就是我最终要做的。这是使用 640x49 PNG 的混合,这是我需要的蓝色“突出显示”背景的颜色。

    在 AppDelegate.swift 中:

    var selectedBG = UIImage(named:"tab-selected-full")?.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    UITabBar.appearance().selectionIndicatorImage = selectedBG
    

    然后在第一个加载的视图控制器中,我有:

    tabBarController?.tabBar.frame.size.width = self.view.frame.width+4
    tabBarController?.tabBar.frame.origin.x = -2
    

    上面两行的原因是,默认情况下,Apple 在标签栏的左右两侧与标签栏项目之间有一个 2px 的边框。

    在上面我只是让标签栏宽 4px,然后将其偏移,使左侧的边框刚好落在视图之外,因此右侧的边框也将落在视图之外。

    【讨论】:

    • UIEdgeInsetsMake(0, 0, 0, 0) 相当于UIEdgeInsetsZero 我相信。
    猜你喜欢
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    相关资源
    最近更新 更多