MESegmentedControl is an enhanced UISegmentedControl which allows you to set badge numbers on each of the control's segments, like so:
- Copy the following files from the MESegmentedControl directory into your project. Link them to your target.
- MESegmentedControl.h
- MESegmentedControl.m
- CustomBadge.h
- CustomBadge.m
- MESegmentedControl should use ARC but CustomBadge should not. So if your project does use ARC, set the compiler flag
-fno-objc-arc
for the CustomBadge.m source within your target. If your project doesn't use ARC, set the compiler flag-fobjc-arc
for the MESegmentedControl.m source within your target. See illustrated instructions here.
- Within your application, replace instances of UISegmentedControl with MESegmentedControl.
- Set badge numbers using, e.g.
[segmentedControl setBadgeNumber:1 forSegmentAtIndex:0]
. Set a badge number to 0 to remove the badge. Assign custom colours to badges using blocks - see MEViewController.m. - Consider setting your badge numbers in your view controller's viewWillAppear method and clearing them using
[segmentedControl clearBadges]
in viewDidDisappear. - MESegmentedControl should not be placed directly on a UINavigationItem due to issues during push / pop transitions. If you wish to use MESegmentedControl within a UINavigationItem (as in the screenshot above), then do not do so directly. Instead, place a transparent UIView onto the UINavigationItem and place the MESegmentedControl onto the transparent UIView.
- If you need to add or remove segments after adding your first badge, then call
[segmentedControl clearBadges]
beforehand. It's not sufficient here to simply set your badges to zero. - To get the current badge number of a segment, call
NSUInteger i=[segmentedControl getBadgeNumberForSegmentAtIndex:0]
.
Open the included XCode project to see a sample app.
- Segments must each be the same size.
- Badges can only take positive integer values.
Neither of these limitations are inherent to the platform: they may be resolved with straightforward enhancements to the library.
MESegmentedControl is distributed under the Modified BSD License.
- MESegmentedControl was written by Dave Thompson.
- The CustomBadge component was written by Sascha Paulus. See Sascha's blog.