Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my UISegmentControl segments highlighting in iOS 8 when I have set background images for all states?

In iOS 6/7, I have used UISegmentedControl with background images to create an effect like so:

Customized UISegmentedControl

I accomplished this by setting the background image for the UISegmentedControl for each of standard states, like so:

UIImage *segmentedControlBackgroundImage = [UIImage imageNamed:@"profileSegmentedControlBackground"];
UIImage *segmentedControlBackgroundSelectedImage = [UIImage imageNamed:@"profileSegmentedControlBackgroundSelected"];

[self.segmentedControl setBackgroundImage:segmentedControlBackgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:segmentedControlBackgroundImage forState:UIControlStateDisabled barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:segmentedControlBackgroundSelectedImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:segmentedControlBackgroundSelectedImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

When a segment becomes select or is highlighted, it has the nice blue bar underneath and I set text attributes to change the text color to be blue. There's some extra code for the dividers, but I think that's unrelated so I've omitted it.

My problem is that in iOS 8, there are a couple of actions that cause the background of the segment to turn gray and look bad; One being when you change your selection, the cell you tapped turns gray until the transition completes, and the other is that if you tap and hold an already selected segment, it turns gray. Both look identical and can be seen below.

UISegmentedControl with strange highlighting

Some extra pieces of possibly relevant information:

  • The tintColor for the segmentedControl is clear
  • I'm not subclassing UISegmentedControl
  • I haven't changed any properties for UISegmentedControl using its appearance proxy
  • I am using 1 point wide images for the background images and UISegmentedControl is automatically determining the capInsets and tiling the image
like image 961
Acey Avatar asked Sep 29 '14 23:09

Acey


People also ask

How do you change the segment control color?

To change the color/font of the selected segment titles, use setTitleTextAttributes with a state of . selected / UIControlStateSelected . If you create a segmented control with images, if the images are created as template images, then the segmented control's tintColor will be used to color the images.

When would you use segmented control?

A segmented control is a linear set of segments, each of which functions as a button that can display a different view. Use a segmented control to offer choices that are closely related but mutually exclusive. A tab bar gives people the ability to switch between different subtasks, views, or modes in an app.

What is segment control in Swift?

“Segment control is a set of two or more segments, which is used to perform multiple tasks on the same screen. Segment working same as a button.”


2 Answers

Just in case, in Swift that would be (UPD for Swift 4)

segmentedControl.setBackgroundImage(image, forState: .selected, barMetrics:.Default)

segmentedControl.setBackgroundImage(image, forState: .highlighted, barMetrics:.Default)

like image 58
nikans Avatar answered Jan 18 '23 00:01

nikans


The reason the segment turns grey on selecting an already selected segment is because the segmented control is missing the state for selected and highlighted at the same time.

In your case calling:

[self.segmentedControl setBackgroundImage:segmentedControlBackgroundSelectedImage forState:UIControlStateSelected | UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

should fix that problem.

when you change your selection, the cell you tapped turns gray until the transition completes

I couldn't reproduce that one, but perhaps this will fix both issues.

like image 30
GraemeArthur Avatar answered Jan 17 '23 23:01

GraemeArthur