cfisher cfisher - 2 years ago 189
Objective-C Question

Adding a UISwitch to a UINavigationItem

I want to have a UISwitch in the right corner of a UINavigationItem, instead of a UIBarButton. Is this possible without subclassing the UINavigationItem?

Answer Source

You can create a UIBarButtonItem with a custom view inside it by using the initWithCustomView: method. For example:

UISwitch *switch = ...;
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:mySwitch];

Set this as the left/right item on your UINavigationItem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download