Reputation: 79
I've created a segmented control for my app that consists of 4 segments. I know how to change a background color for the all four of them but I want each segment to have their own color:
1st segment - blue background color
2nd segment - red background color
etc.
Is there any way to achieve it in swift?
Upvotes: 0
Views: 1049
Reputation: 958
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
Upvotes: 1