UISegmentedControl Behavior

Quick question about the UISegmentedControl class on iPhone.

Hopefully, some of them might have noticed how, in the default state with two segments, the segmented control will still switch, even if the user clicks on the selected segment.

I have seen UISegmentedControls in applications that deny this behavior and do not switch when the user clicks on the selected segment. Namely, the Apple iTune Store app.

Is there an easy way to prevent this behavior that I am missing or do I need to write some logic in valueChanged?

I am trying to disable segments currently selected and enable unselected ones, but this seems to change the appearance of the control when I don't want it.

Any ideas?

+4
source share
4 answers

Take a look at the header file UISegmentedControl.h (the simple way is to double-click on “UISegmentedControl” in Xcode with the command held down).

Here you will see several elements in the _segmentedControlFlags structure. If you create a category in UISegmentedControl, you can control any of these elements, including dontAlwaysToggleForTwoSegments , which, as you might guess by name, is what you are looking for.

Since this is documented in the API header (although not in the actual documentation), it should be fairly safe to use.

+8
source

This default behavior has been changed in API 3.0.

+4
source

In 3.0, " _segmentedControlFlags " was changed to an instantaneous property, which can still be found in the UISegmentedControl.h header file.

+4
source

Access to _segmentedControlFlags through a category is seen as using a private API and therefore violating the Apple License Agreement, as I should have known today. Until now, I thought Categories were a language feature to fix someone's awful class design ...

+1
source

All Articles