UISegmentedControl TintColor 到渐变色

UISegmentedControl TintColor to Gradient Color

我正在尝试将所选段的 UIsegmentedControl 色调颜色设置为渐变颜色,但我无法执行此操作

我正在尝试关注这篇文章https://www.bethedev.com/2019/02/set-gradient-tint-color-for-segmented.html

正在尝试使用此代码:

segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.white],for: UIControl.State.normal)
        segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.white],for: UIControl.State.selected)  

fileprivate func updateGradientBackground() {
        let sortedViews = segmentedControl.subviews.sorted( by: { [=10=].frame.origin.x < .frame.origin.x } )
        for (_, view) in sortedViews.enumerated() {
//            let gradientImage = gradient(size: segmentedControl.frame.size, color: [UIColor.cyan,UIColor.blue])!
            view.backgroundColor = UIColor(patternImage: UIImage(named: "segmentedRectangle.png")!)
            view.tintColor = UIColor.clear
        }
    }

我希望只有一个片段具有 segmentedRectangle.png 图像颜色,但它显示在整个分段控件上,如 this.

试试这段代码,我在相关部分发表了评论。如果您需要更多解释,请告诉我。

 let segmentedControl: UISegmentedControl = {
       let view =  UISegmentedControl(items: ["Pounds", "Kilograms"])
        view.selectedSegmentIndex = 0
        view.tintColor = .black
        view.backgroundColor = .white
        view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width - 40, height: 20)
        /// Gradient
        let gradient = CAGradientLayer()
        gradient.frame =  CGRect(x: 0, y: 0, width:  UIScreen.main.bounds.width - 40, height: 20)
        let leftColor = UIColor.red
        let rightColor = UIColor.purple
        gradient.colors = [leftColor.cgColor, rightColor.cgColor]
        gradient.startPoint = CGPoint(x: 0, y: 0.5)
        gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
        /// Create gradient image
        UIGraphicsBeginImageContext(gradient.frame.size)
        gradient.render(in: UIGraphicsGetCurrentContext()!)
        let segmentedControlImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        // Normal Image
        let rect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size);
        let context:CGContext = UIGraphicsGetCurrentContext()!;
        context.setFillColor(UIColor.white.cgColor)
        context.fill(rect)
        let normalImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        /// Set segmentedControl image
        view.setBackgroundImage(normalImage, for: .normal, barMetrics: .default)
        view.setBackgroundImage(segmentedControlImage, for: .selected, barMetrics: .default)
        return view
    }()

用法: 在您的 ViewDidLoad 上,将 navigationItem 标题视图设置为您的分段控件,如下所示:-


self.navigationItem.titleView = segmentedControl

我认为很少有 modifications/custominization 你可以得到你想要的,干杯 :)

StoryBoard/InterfaceBuilder

只需在您的 ViewDidLoad 中调用它并在函数调用中传递您的出口名称:-

   func configureSegementedControl(segmentedControl: UISegmentedControl) {
        segmentedControl.selectedSegmentIndex = 0
        segmentedControl.tintColor = .black
        segmentedControl.backgroundColor = .white
        segmentedControl.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width - 40, height: 20)
        /// Gradient
        let gradient = CAGradientLayer()
        gradient.frame =  CGRect(x: 0, y: 0, width:  UIScreen.main.bounds.width - 40, height: 20)
        let leftColor = UIColor.red
        let rightColor = UIColor.purple
        gradient.colors = [leftColor.cgColor, rightColor.cgColor]
        gradient.startPoint = CGPoint(x: 0, y: 0.5)
        gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
        /// Create gradient image
        UIGraphicsBeginImageContext(gradient.frame.size)
        gradient.render(in: UIGraphicsGetCurrentContext()!)
        let segmentedControlImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        // Normal Image
        let rect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size);
        let context:CGContext = UIGraphicsGetCurrentContext()!;
        context.setFillColor(UIColor.white.cgColor)
        context.fill(rect)
        let normalImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        /// Set segmentedControl image
        segmentedControl.setBackgroundImage(normalImage, for: .normal, barMetrics: .default)
        segmentedControl.setBackgroundImage(segmentedControlImage, for: .selected, barMetrics: .default)
    }