0

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?

  • try to call this method func setBackgroundImage(_ backgroundImage: UIImage?, forState state: UIControlState, barMetrics barMetrics: UIBarMetrics) and each segment having lable change that background – Ramkumar chintala Sep 03 '15 at 14:23
  • @Ramkumarchintala I'm pretty new to swift (started learning about two weeks ago) can you go into specifics please? – Arsenii Nibble Sep 03 '15 at 14:45
  • @Arsnii nibble try below mentioned way. otherwise take a toolBar With BarButton Iteams. look like a segment Control – Ramkumar chintala Sep 04 '15 at 06:14

1 Answers1

1

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
Ramkumar chintala
  • 958
  • 11
  • 24