如何在分段控制中改变单个线段的颜色



我为我的应用程序创建了一个分段控件,由4段组成。我知道如何更改这四个部分的背景颜色但我想让每个部分都有自己的颜色:

第一段-蓝色背景色

第二段-红色背景色

等。

有办法在swift中实现吗?

试试下面的代码

func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}

使用下面的方法对每个片段设置图像(上面的方法给出带有颜色的图像)

setImage(image: UIImage?, forSegmentAtIndex segment: Int) // can only have image or title, not both. must be 0..#segments - 1 (or ignored). default is nil

最新更新