如何为 carbonTabSwipeNavigation CarbonKit Cocoapods 设置颜色渐变
Posted
技术标签:
【中文标题】如何为 carbonTabSwipeNavigation CarbonKit Cocoapods 设置颜色渐变【英文标题】:how to set color Gradient for carbonTabSwipeNavigation CarbonKit Cocoapods 【发布时间】:2019-02-14 08:35:26 【问题描述】:我是 ios 新手。我想在 CarbonTabSwipeNavigation 上应用渐变颜色。我尝试将渐变应用到 CarbonTabSwipeNavigation 的工具栏,但没有发生 我尝试过使用静态颜色代码
carbonTabSwipeNavigation.toolbar.isTranslucent = true
var color1 = hexStringToUIColor(hex: "#00275E")
carbonTabSwipeNavigation.carbonSegmentedControl?.backgroundColor = color1
carbonTabSwipeNavigation.setIndicatorColor(UIColor.white) //tabBar font
carbonTabSwipeNavigation.setSelectedColor(UIColor.white, font: UIFont.boldSystemFont(ofSize: 14))
【问题讨论】:
考虑将答案标记为正确以扩大问题的可见性:***.com/help/someone-answers 【参考方案1】:首先,在您的项目中添加这些扩展。
extension UINavigationBar
func setGradientBackground(colors: [UIColor])
var updatedFrame = bounds
updatedFrame.size.height += self.frame.origin.y
let gradientLayer = CAGradientLayer(frame: updatedFrame, colors: colors)
setBackgroundImage(gradientLayer.createGradientImage(), for: UIBarMetrics.default)
extension UIView
func setGradientBackgroundOnView(colors: [UIColor])
var updatedFrame = bounds
// updatedFrame.size.height += self.frame.origin.y
let gradientLayer = CAGradientLayer(frame: updatedFrame, colors: colors)
self.layer.insertSublayer(gradientLayer, at: 0)
extension CAGradientLayer
convenience init(frame: CGRect, colors: [UIColor])
self.init()
self.frame = frame
self.colors = []
for color in colors
self.colors?.append(color.cgColor)
startPoint = CGPoint(x: 0, y: 1)
endPoint = CGPoint(x: 1, y: 1)
func createGradientImage() -> UIImage?
var image: UIImage? = nil
UIGraphicsBeginImageContext(bounds.size)
if let context = UIGraphicsGetCurrentContext()
render(in: context)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
然后根据您的要求调用此方法。
示例:- 如果您想在应用的navigationbar
上设置gradient
颜色,请调用使用此
self.navigationController?.navigationBar.setGradientBackground(colors: [UIColor.black, UIColor.red])
【讨论】:
以上是关于如何为 carbonTabSwipeNavigation CarbonKit Cocoapods 设置颜色渐变的主要内容,如果未能解决你的问题,请参考以下文章
如何为 RecyclerView 设置 onItemClickListener? [复制]