I'm trying to make a UIBarButtonItem with a UIButton as its custom view. When I make it, it doesn't show up. Here is how I'm declaring the button:
self.optionsButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.optionsButton setFrame:CGRectMake(0, 0, 30, 20)];
[self.optionsButton setTitle:@"Options" forState:UIControlStateNormal];
[self.optionsButton addTarget:self
action:@selector(presentOptions)
forControlEvents:UIControlEventTouchUpInside];
[self.optionsButton setTintColor:[UIColor clearColor]];
[self.optionsButton setBackgroundColor:[[UIColor blueColor] colorWithAlphaComponent:0.1] forUIControlState:UIControlStateSelected];
[self.optionsButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.optionsButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
self.optionsButton.layer.cornerRadius = 5;
self.optionsButton.clipsToBounds = YES;
[self.optionsButton sizeToFit];
And here is how I'm declaring the UIToolbar
self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 44)];
NSMutableArray *items = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpaceBefore = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:flexSpaceBefore];
[items addObject:[[UIBarButtonItem alloc] initWithCustomView:self.optionsButton]];
UIBarButtonItem *flexSpaceAfter = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:flexSpaceAfter];
[self.toolbar setItems:items];
[self.view addSubview:self.toolbar];
CGRect toolbarFrame = self.toolbar.frame;
toolbarFrame.origin.y = self.view.frame.size.height - toolbarFrame.size.height - self.view.frame.origin.y;
toolbarFrame.size.width = self.view.frame.size.width;
self.toolbar.frame = toolbarFrame;
Here are a list of things I've tried based on other posts that have not worked:
- One Two Three Four: Using the navigation controller toolbar and
[NavController setToolbarItems
- it showed me the toolbar (I made sure mine was no longer being displayed) but no button - One: Making sure the button has a valid frame - Clearly from my code you can see that the button has a valid frame that fits the text
So, if anyone has any ideas of why this isn't working I'd really appreciate the help. At this point I'm basically hoping that it's something really stupid that I've overlooked since I don't want to keep working on this for so long :(
EDIT:
The toolbar and button are initially declared as
@property (nonatomic, strong) UIButton *optionsButton;
@property (nonatomic, strong) UIToolbar *toolbar;