I am the maintainer of STAControls which subclasses various UIControl
s, 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:
While in iOS 12 and prior versions of iOS this performs just fine:
Any idea(s) on how to work around this?
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With