简体   繁体   中英

UIBarButtonItem created with UIButton not showing up in UIToolbar

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;

After Reviewing your code. I found that if you keep button property as (weak), button will not be added in toolbar. So keep your button property as (strong,nonatomic)

@property(strong,nonatomic) UIButton *optionsButton

As Strong means that as long as this property points to an object, that object will not be automatically released. In non-ARC it's a synonym for retain

what is the difference between strong and weak property?

I figured it out! Like I was hoping, it was something stupid. I was setting up the button directly in viewDidLoad , and the toolbar in a helper function. I had thought that I was calling the helper function before declaring the button, but it turns out I was calling it before the button even got declared... Lesson learned: use breakpoints and the Xcode debugger more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM