Adding Image And title both to UIsegmentControl IOS

2.7k views Asked by At

Is there a way so that you can set both image and title of UISegmentControl simultaneously, so that image appear next to the title , just like image appear next to title in UIButton.

I am trying but if I set image of Selected segment of UISegmentControl then title disappears and if I set title of Selected segment of UISegmentControl then Image disappears, I want to set both at a time.

1

There are 1 answers

0
Andrew Monshizadeh On BEST ANSWER

The official documentation for -setImage:forSegmentAtIndex: says

A segment can only have an image or a title; it can’t have both. There is no default image.

So, no, there is no way to do what you want using the image and title properties.

However, there are a few options to accomplish the objective in different ways.

  • if you are going to use the same image for each segment, you could use the appearance methods to set the background and then use the method -setContentOffset:forSegmentAtIndex: to move the title to the side of the background image.
  • you could create an image that had both the icon and the text in it. This is less than ideal as a change to the text requires exporting of the entire asset again. But is an option.
  • the least ideal would be to fake a segmented control using buttons that you write the code for to handle state changes. There may even be some open source options that do just that, so feel free to search for them.