gpt4 book ai didi

ios - 如何为 carbonTabSwipeNavigation CarbonKit Cocoapods 设置颜色渐变

转载 作者:行者123 更新时间:2023-11-28 07:31:00 25 4
gpt4 key购买 nike

我是 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))

最佳答案

首先,在您的项目中添加这些扩展。

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])

关于ios - 如何为 carbonTabSwipeNavigation CarbonKit Cocoapods 设置颜色渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54686132/

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