ios - UISegmentedControl TintColor 到渐变色

标签 ios swift tintcolor

我正在尝试将所选段的 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: { $0.frame.origin.x < $1.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 标题 View 设置为您的分段控件,如下所示:-


self.navigationItem.titleView = segmentedControl

Output

我认为只需进行少量修改/定制,您就可以得到想要的东西,干杯 :)

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

关于ios - UISegmentedControl TintColor 到渐变色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57573809/

相关文章:

swift - 通过搜索栏创建到另一个 View Controller 的 segue?

ios - 更改关闭状态和边框颜色的 UISwitch 默认颜色

ios - 如何为 iOS 的 UIWebView 中加载的文档(doc、html)创建缩略图

ios - 多个自定义 View 时的自动布局问题

ios - 应用商店评论 : my app requires a UK IP address

ios - 快速检测 UILabel 文本更改

ios - 如何更改 UISwitch 关闭状态的默认颜色?

ios - 如何在不更改导航栏色调的情况下更改底部工具栏色调颜色?

ios - 从 UIView ios 推送 View Controller

ios - Fabric Beta iOS 更新通知未出现