gpt4 book ai didi

ios - 如何通过以编程方式创建 UIImage 来更改 UITabBar 顶部边框的颜色?

转载 作者:IT王子 更新时间:2023-10-29 05:32:29 26 4
gpt4 key购买 nike

据我了解,更改顶部边框颜色的唯一方法是设置背景图像(320x49,像素线在顶部)。在我看来,这是唯一的方法(如果我错了请纠正我)。

有没有办法使用图像文件来做到这一点?例如,有人通过从代码创建 UIImage 来帮助我更改 NavigationBar 底部边框:

UINavigationBar.appearance().shadowImage = UIImage.colorForNavBar(UIColor.redColor())

extension UIImage {
class func colorForNavBar(color: UIColor) -> UIImage {
let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()

CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)

let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return image
}
}

这个解决方案实际上效果很好;它改变了我底部边框的颜色。

我尝试将其应用于 TabBar,但没有任何变化。

UITabBar.appearance().shadowImage = UIImage.colorForNavBar(.redColor())

最佳答案

您几乎已经回答了您自己的问题。您可以对 UITabBar 执行与对 UINavigationBar 执行的操作相同的操作。如果你想改变阴影图像(即“顶部边框”),那么你必须改变背景图像。直接来自Apple :

The custom shadow image for the tab bar. This attribute is ignored if the tab bar does not also have a custom background image. To set this attribute programmatically, use the shadowImage property.

在你自己的问题中你似乎意识到了这一点:

the only way to change the color of the top border is to set the background image (320x49, with pixel line at top)

除了顶部有一条线的不是背景图像。您只需将背景图像设置为任何内容,然后您就可以根据自己的喜好设置阴影图像。

如果您在 Xcode 中打开简单的“选项卡式应用程序”模板,您会发现添加这两行代码(以及您的 UIImage 扩展代码)确实有效:

// White background with red border on top
UITabBar.appearance().backgroundImage = UIImage.colorForNavBar(.whiteColor())
UITabBar.appearance().shadowImage = UIImage.colorForNavBar(.redColor())

Tab Bar

关于ios - 如何通过以编程方式创建 UIImage 来更改 UITabBar 顶部边框的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37580148/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com