cfisher cfisher - 6 months ago 30
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

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.