1

I am trying to create a toolbar programmatically in a class that subclasses ABPersonViewController. Here is what I have done.

UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered  target:self     action:@selector(onToolbarTapped:)];
NSArray *items = [NSArray arrayWithObjects: customItem, nil];
[self.navigationController.toolbar setItems:items animated:NO];
//[self setToolbarItems:[NSArray arrayWithObject:items]];
self.navigationController.toolbar.barStyle = UIBarStyleBlackOpaque;
[self.navigationController setToolbarHidden:NO animated:YES];

The toolbar doesn't appear. What am I doing wrong here.

Edit: I have edited my code as follows

UIToolbar *toolbar = [[[UIToolbar alloc] init]autorelease];
toolbar.barStyle = UIBarStyleBlackOpaque;
toolbar.tintColor = [UIColor blackColor];
toolbar.frame = CGRectMake(0, 372, self.view.frame.size.width, 45);
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:unblockContact style:UIBarButtonItemStyleBordered   target:self     action:@selector(onToolbarTapped:)];
customItem.tintColor = [UIColor blackColor];
NSArray *items = [NSArray arrayWithObjects:customItem, nil];
[toolbar setItems:items animated:NO];
[self.view addSubview:toolbar];
[customItem release];

How can I make the barbuttonitem occupy the entire toolbar. The user shouldn't feel that there is a barbuttonitem on the toolbar. Could I do this way, or is there an alternate way? Help needed. Thanks.

Xavi Valero
  • 2,047
  • 7
  • 42
  • 80

1 Answers1

1

UIToolbar is a subclass of UIView, you should add it as any other view, following method is what am successfully using,

UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 45);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithObjects...]];
[toolbar setItems:items animated:NO];
[self.view addSubview:toolbar];
Jaume
  • 3,672
  • 19
  • 60
  • 119