An enhanced UISegmentedControl which allows badge numbers to be assigned to segments.
Objective-C
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
MESegmentedControl.xcodeproj
MESegmentedControl
.gitignore
README.md
SampleScreenShot.png

README.md

Introduction

MESegmentedControl is an enhanced UISegmentedControl which allows you to set badge numbers on each of the control's segments, like so:

First Screnshot

Installation

  1. Copy the following files from the MESegmentedControl directory into your project. Link them to your target.
    1. MESegmentedControl.h
    2. MESegmentedControl.m
    3. CustomBadge.h
    4. CustomBadge.m
  2. 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.

Usage

  1. Within your application, replace instances of UISegmentedControl with MESegmentedControl.
  2. 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.
  3. Get current badge numbers using, e.g. [segmentedControl getBadgeNumberForSegmentAtIndex:0].
  4. Consider setting your badge numbers in your view controller's viewWillAppear method and clearing them using [segmentedControl clearBadges] in viewDidDisappear.
  5. 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.
  6. 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.

Open the included XCode project to see a sample app.

Limitations

  • 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.

License

MESegmentedControl is distributed under the Modified BSD License.

Credits

  • MESegmentedControl was written by Dave Thompson.
  • The CustomBadge component was written by Sascha Paulus. See Sascha's blog.