UISegmentedControl Doesn't Deselect Old Segment

uisegmentedcontrol tutorial
uisegmentedcontrol programmatically
swift segmented control programmatically
uisegmentedcontrol text color
custom segmented control swift 4
uisegmentedcontrol image and text swift
uisegmentedcontrol remove border
uisegmentedcontrol subviews

I have a UISegmentedControl in a UITableView Table row. The selection fires, and the new segment is highlighted. The problem is, the old segment remains selected.

If I close and reopen the popover that contains the table, the correct segment index is displayed.

I'm running XCode 6.1 and testing on iOS 7.1 simulator.

Assistance appreciated.

UITableViewCell *segmentCell = [tableView dequeueReusableCellWithIdentifier:segmentCellIdentifier];
if (segmentCell == nil) {
segmentCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:segmentCellIdentifier];
    }
segmentCell.backgroundColor = [UIColor clearColor];
segmentCell.backgroundView = nil;
NSArray *segmentItems = [NSArray arrayWithObjects: NSLocalizedString(@"settingsBackgroundCork", @"Cork - Select cork background theme"), NSLocalizedString(@"settingsBackgroundDark", @"Dark - Select dark background theme"), NSLocalizedString(@"settingsBackgroundLight", @"Light - Select light background theme"), nil];
self.backgroundSegmentedControl = [[UISegmentedControl alloc] initWithItems: segmentItems];
[self.backgroundSegmentedControl addTarget:self action:@selector(backgroundControlChanged:) forControlEvents: UIControlEventValueChanged];
self.backgroundSegmentedControl.frame = CGRectMake(10, 6, 300, 32);
NSInteger background = [[NSUserDefaults standardUserDefaults] integerForKey:kUserDefaultsBackgroundSelection];
self.backgroundSegmentedControl.selectedSegmentIndex = background;
self.backgroundSegmentedControl.momentary = NO;
[segmentCell.contentView addSubview:self.backgroundSegmentedControl];
cellToReturn = segmentCell;

Here is the method that gets called on segment selection:

- (void)backgroundControlChanged:(UISegmentedControl *)control
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:control.selectedSegmentIndex forKey:kUserDefaultsBackgroundSelection];
self.backgroundSegmentedControl.selectedSegmentIndex = control.selectedSegmentIndex;
[self.backgroundSegmentedControl setNeedsDisplay];
[[NSNotificationCenter defaultCenter] postNotificationName:kPageBackgroundShouldChangeNotification object:nil];
}

IOS UISegmentedControl doesn't not display in ios 10, The problem is that you are not giving the segmented control any size. In iOS 11 the title view is sized internally by autolayout, but not in iOS 10  The “tintColor” property introduced in iOS 7 allowed to change the appearance of UISegmentedControl. Starting with iOS 13 “tintColor” does nothing. No matter what color you will assign, the

I was able to fix this issue by moving my call to setSelectedSegmentIndex to inside the setNeedsLayout function, instead of calling it right after creation of the UISegmentedControl. Of course I also had to create a variable to keep track of which segment should be selected.

iOS 13 UISegmentedControl: 3 important changes, Tint color does not work anymore. The “tintColor” property introduced in iOS 7 allowed to change the appearance of UISegmentedControl. A UISegmentedControl is a horizontal bar which consists of discrete Buttons. These buttons are used to show/hide content or to show different contents when each of the Buttons is clicked. This way each Button is responsible for a different Segment on the screen. Following are the helper functions for a UISegmentedControl:

Had the same issue. Resolved it by removing the previous instance of the segmentedControl from the superview before adding the new instance.

Adding the below-mentioned 3 lines of code to remove previous segmentedControl before adding the new instance of segmentedControl to the parent view.

self.backgroundSegmentedControl.tag = 101
if let foundView2 = segmentCell.contentView.viewWithTag(101) {
    foundView2.removeFromSuperview()
}
[segmentCell.contentView addSubview:self.backgroundSegmentedControl];

iOS 13 Segmented Control Design Problem: How To Solve It , First of all, let's explain what a Segmented Control axctually is. UISegmentedControl is a button-like object in Xcode. More precisely, it's a set of​  The UISegmentedControl provided by UIKit is a ubiquitous control. However, when it comes to customizing this control, it can get pretty tricky. So I’m going to try to explain how this widget can be styled to better match your app by walking through a few different customizations. The image below shows the different styles we will build.

Building a SegmentedViewController, Note: This is only a visual indication it doesn't trigger any callbacks. Then we call addTarget to register an action to be called when the value of the segmented  When the UISegmentedControl is supposed to be disabled, simply show the UIView and disable the user interaction on the UISegmentedControl. When the UISegmentedControl is supposed to be enabled, simply hide the UIView and enable the user interaction on the UISegmentedControl. In both cases do not change the UISegmentedControl.enabled property.

Segmented Control Accessibility, That problem is that UISegmentedControl doesn't offer good VoiceOver accessibility. This is unusual for Apple as their accessibility stuff is  So I tried to change the text attribute of the title of my UISegmentedControl, but it doesn't work, nothing change. I have also applied a custom background and divider and it works correctly, but not

How to create a segmented control and read values from it, SwiftUI's Picker can also be used to create segmented controls equivalent to UISegmentedControl from UIKit, although it needs to be bound to  iOS 13 segments ignore the tint colour for text in segments and the selected "bubble". They use the tint colour for images. The selectedSegmentTintColor can be used to change the colour of the bubble. You can also change the.normal and.selected text attributes to control the text colour.

Comments
  • Does your table view scroll, and if so, do you only see this effect after you've scrolled?
  • The tableview is scroll capable. That said this element is at the top, and visible without requiring any scrolling to be displayed. I'm not scrolling when I test this (i.e. row is not being redrawn).
  • The only way I could duplicate your problem was with scrolling. One problem you code has, is that you will be adding new segmented controllers on top of old ones when you reuse a cell. It would be better to create a subclassed cell, and add the segmented control in its init method. Or, you could check if a cell contains a segmented control before adding one in cellFroRowAtIndexPath.
  • I'm having the same weird issue. The result is a little different if I put the segmented controls' creation in a dispatch_async(dispatch_get_main_queue(), ^{}) block, but it still doesn't behave correctly.. I resorted to making a similar control completely from scratch. This is indeed a strange issue.
  • Thanks for the suggestion. I've tried your first 2 suggestions, to no avail. Updated code example above. I also tried your -1 to turn off the selection, but then the segmented control doesn't register the selection at all.