Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying UISegmentedControlNoSegment to UISegmentedControl's selectedSegmentIndex has no Effect on iOS 13

I am the maintainer of STAControls which subclasses various UIControls, one of which is UISegmentedControl. Within that project's sample app, I have the following code (can be downloaded from the aforementioned link):

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSInteger integer = [textField.text integerValue];
    if (integer < 0 || integer >= self.segmentedControl.numberOfSegments) { this doesn't work
        self.segmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment;
    } else { // this works
        self.segmentedControl.selectedSegmentIndex = integer;
    }
}

In iOS 13 the UISegmentedControlNoSegment assignment is not respected:

enter image description here

While in iOS 12 and prior versions of iOS this performs just fine:

enter image description here

Any idea(s) on how to work around this?

like image 200
Stunner Avatar asked Sep 12 '19 10:09

Stunner


1 Answers

The solutions given in the other answers are overblown. There is a much simpler workaround: just call setNeedsLayout on the segmented control.

self.seg.selectedSegmentIndex = UISegmentedControl.noSegment
self.seg.setNeedsLayout()
like image 102
matt Avatar answered Oct 23 '22 04:10

matt