0

How can I add a custom UIControl such as https://github.com/mattlawer/MBSwitch to a navigation bar? Specifically, I need it to be the second button bar item on the left side.

My reason for attempting this is that I have a need to have a resized UISwitch with an off state custom background color as a second item on left of a Nav bar. I can resize the switch, but if I want to set the background color I run into problems as discussed in: How do I make a UISwitch under iOS 7 not take the background colour of the view behind it?

Community
  • 1
  • 1
bhartsb
  • 1,316
  • 14
  • 39

1 Answers1

1

You should be able to use initWithCustomView:. In this case the button won't handle any taps so you should use the switch interaction for all state changes.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • The leftbuttonbar already has one button added in storyboard, so what about that? Also I tried [self.navigationItem.leftBarButtonItems arrayByAddingObject:filterSwitch]; to no success. – bhartsb Jul 20 '16 at 19:50
  • you need to add all the buttons into one array. if you only have one item then it's in the other property and not in the array option. it's weird and counterintuitive, but that's the way it is... – Wain Jul 20 '16 at 19:56
  • tried [_switchButtonBarItem initWithCustomView:filterSwitch]; but it didn't work. The first leftbuttonitem is all that is there. I.e. the filterSwitch is what I want to be my second item. – bhartsb Jul 20 '16 at 19:57
  • This is what seemed to work with the one item already added in storyboard: `UIBarButtonItem *switchButtonBarItem = [[UIBarButtonItem alloc]initWithCustomView:_filterSwitch]; NSArray * a = [self.navigationItem.leftBarButtonItems arrayByAddingObject:switchButtonBarItem]; self.navigationItem.leftBarButtonItems = a;` – bhartsb Jul 20 '16 at 20:32