ios - 自定义 UISegmentedControl : how change tint color of selected segment's image?

标签 ios swift uisegmentedcontrol

我用过这个extension对于 UISegmentedControl 其工作方式如您所见 here.

但我希望 UISegementedControl 的工作方式略有不同。当我选择一个片段时,我希望所选片段的图像将颜色更改为 colorGreyLight 而不是将背景颜色更改为 colorGreyDark 并将所选片段图像更改为 UIColor.clearColor()(如你所见 here )

那我该怎么做呢?

我的代码

UISegmented 控件扩展:

extension UISegmentedControl {
    func removeBorders() {
        setBackgroundImage(imageWithColor(UIColor.clearColor()), forState: .Normal, barMetrics: .Default)
        setBackgroundImage(imageWithColor(tintColor), forState: .Selected, barMetrics: .Default)
        setDividerImage(imageWithColor(UIColor.clearColor()), forLeftSegmentState: .Normal, rightSegmentState: .Normal, barMetrics: .Default)
    }

    // create a 1x1 image with this color
    private func imageWithColor(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
    }
}

UI分段控制:

    let types = ["rectangle", "circle", "triangle"]
    shapeSelector = UISegmentedControl(items: types)
    shapeSelector.frame = CGRectMake(0, 0, sliderHeight*3 + 30, sliderHeight)
    shapeSelector.center = CGPoint(x: drawToolbar.bounds.width/2, y: sliderCenterY)
    shapeSelector.tintColor = colorGreyDark
    shapeSelector.removeBorders()
    shapeSelector.selectedSegmentIndex = 0 //default: select rectangle
    shapeSelector.addTarget(self, action: "selectShape:", forControlEvents: .ValueChanged)
    shapeSelector.setImage(UIImage(named: "rectangle"), forSegmentAtIndex: 0)
    shapeSelector.setImage(UIImage(named: "circle"), forSegmentAtIndex: 1)
    shapeSelector.setImage(UIImage(named: "triangle"), forSegmentAtIndex: 2)
    shapeSelector.contentMode = UIViewContentMode.ScaleAspectFit
    drawToolbar.addSubview(shapeSelector)

如果对我使用的有任何疑问:

  • iOS 9.2
  • swift 2
  • Xcode 7.2

更新 好的,我已经按照 Keyur 的建议更改了我的代码(如下面的评论标记)

UISegmented 控件扩展:

extension UISegmentedControl {
    func removeBorders() {
        setBackgroundImage(imageWithColor(UIColor.clearColor()), forState: .Normal, barMetrics: .Default)
        setBackgroundImage(imageWithColor(UIColor.clearColor()), forState: .Selected, barMetrics: .Default) //change to clear color
        setDividerImage(imageWithColor(UIColor.clearColor()), forLeftSegmentState: .Normal, rightSegmentState: .Normal, barMetrics: .Default)
    }

    // create a 1x1 image with this color
    private func imageWithColor(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
    }
}

UI分段控制:

    let types = ["rectangle", "circle", "triangle"]
    shapeSelector = UISegmentedControl(items: types)
    shapeSelector.frame = CGRectMake(0, 0, sliderHeight*3 + 30, sliderHeight)
    shapeSelector.center = CGPoint(x: drawToolbar.bounds.width/2, y: sliderCenterY)
    shapeSelector.tintColor = colorGreyDark
    shapeSelector.removeBorders()
    shapeSelector.selectedSegmentIndex = 0 //default: select rectangle
    (shapeSelector.subviews[0] as UIView).tintColor = colorGreyLight //added this line
    shapeSelector.addTarget(self, action: "selectShape:", forControlEvents: .ValueChanged)
    shapeSelector.setImage(UIImage(named: "rectangle"), forSegmentAtIndex: 0)
    shapeSelector.setImage(UIImage(named: "circle"), forSegmentAtIndex: 1)
    shapeSelector.setImage(UIImage(named: "triangle"), forSegmentAtIndex: 2)
    shapeSelector.contentMode = UIViewContentMode.ScaleAspectFit
    drawToolbar.addSubview(shapeSelector)

selectShape() 函数:

func selectShape(sender: UISegmentedControl){
    switch sender.selectedSegmentIndex{
    case 0: //rectangle
        shapeType = "rectangle"
        (sender.subviews[0] as UIView).tintColor = colorGreyLight
        (sender.subviews[1] as UIView).tintColor = colorGreyDark
        (sender.subviews[2] as UIView).tintColor = colorGreyDark
    case 1: //circle
        shapeType = "circle"
        (sender.subviews[0] as UIView).tintColor = colorGreyDark
        (sender.subviews[1] as UIView).tintColor = colorGreyLight
        (sender.subviews[2] as UIView).tintColor = colorGreyDark
    case 2: //triangle
        shapeType = "triangle"
        (sender.subviews[0] as UIView).tintColor = colorGreyDark
        (sender.subviews[1] as UIView).tintColor = colorGreyDark
        (sender.subviews[2] as UIView).tintColor = colorGreyLight
    default:
        shapeType = "rectangle"
        (sender.subviews[0] as UIView).tintColor = colorGreyLight
        (sender.subviews[1] as UIView).tintColor = colorGreyDark
        (sender.subviews[2] as UIView).tintColor = colorGreyDark
    }
}

除非我尝试过,否则所选形状并不总是突出显示的形状,如您所见here .它似乎几乎是随机地突出显示形状,我不明白为什么。

如有任何帮助,我们将不胜感激!

最佳答案

(shapeSelector.subviews[2] as UIView).tintColor = UIColor .redColor()
(shapeSelector.subviews[1] as UIView).tintColor = UIColor .redColor()
(shapeSelector.subviews[0] as UIView).tintColor = UIColor .redColor()

关于ios - 自定义 UISegmentedControl : how change tint color of selected segment's image?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34523139/

相关文章:

ios - 为什么网络状态没有快速更新?

iphone - 配置文件名称

ios - 序列化包含未实现 NSCoding 的类的 NSMutableArray

swift - 将大数据从 firestore 加载到 TableView Swift

iphone - 多行 UISegmentedControl

ios - CMSampleBuffer 每次都会自动旋转

ios - 如何在 xcode 7、iOS swift 2 中截取 View 的部分屏幕截图?

swift - 比较 Swift 中的 Objective-C 枚举值

ios - 如何将 UISegmentedControl 与 UITableViewController 结合使用?

swift - 如何修复错误 : this class is not key value coding-compliant for the key tableView. '