Reputation: 1521
Is it possible to add an UIButton
with UIToolbar
? or we can use only UIBarButtonItem
? If so how to add an UIButton
in UIToolbar
?
Upvotes: 1
Views: 1010
Reputation: 20541
you can add UIBarButtonItem
to the UIToolBar
like bellow..
This bellow is an example try this..
Just add this bellow code in viewDidLoad method and then see the UIToolBar
with this two UIBarButtonItems
also here i add Flixiblespace for set these two buttons with right and left corner of UIToolBar
UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSArray *items = [NSArray arrayWithObjects:item1, flexiableItem, item2, nil];
self.toolbarItems = items;
[flexiableItem release];
[items release];
[self.view addSubview:toolbar];
[toolbar release];
Check this links for more information about UIBarButtonItem and UIToolbar_Class
UPDATE:
For your this requirement you can remove that buttons and also add new button instead of old .. see the example..
NSMutableArray *items = [[yourToolbar.items mutableCopy] autorelease];
[items removeObject: yourButtons];
yourToolbar.items = items;
i hope this help you...
Upvotes: 0
Reputation: 3018
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBackButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
[toolBar setItems:[NSArray arrayWithObject:barBackButton]];
Upvotes: 1