gpt4 book ai didi

xcode - 如何更改分段控件中单个分段的颜色

转载 作者:行者123 更新时间:2023-11-30 10:12:09 26 4
gpt4 key购买 nike

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

第一段 - 蓝色背景色

第二段 - 红色背景色

等等

有什么办法可以快速实现吗?

最佳答案

Try below code

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
}

Use below metod for each segment set image (above method give image with color)

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

关于xcode - 如何更改分段控件中单个分段的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32377822/

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