Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a UIBarButtonSystemItem for a UIButton?

I am trying to customize a UIButton and I want to use a UIBarButtonSystemItem style that is available for a UIBarButtonItem object.

How do I use a UIBarButtonSystemItem style on a UIBUtton object?

like image 299
Rashad Avatar asked Jan 26 '14 06:01

Rashad


3 Answers

You can't. UIBarButton and UIButton are two completely different classes. The best you can do is find images close to what you're looking for and add them to UIButtons.

like image 94
Mick MacCallum Avatar answered Nov 06 '22 19:11

Mick MacCallum


Use a UIView instead of a UIButton that contains a UIToolbar with UIBarButtonItems.

UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 44)];
buttonContainer.backgroundColor = [UIColor clearColor];
UIToolbar *dummyBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 80, 44)];

UIBarButtonItem *b1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(doSomething:)];
UIBarButtonItem *b2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(doSomething:)];

NSArray *items = [[NSArray alloc] initWithObjects:b1, b2, nil];

[dummyBar setItems:items];

[buttonContainer addSubview:dummyBar];

...

-(void)doSomething:(id)sender
{
    NSLog(@"Button pushed");
}
like image 43
Jared Price Avatar answered Nov 06 '22 18:11

Jared Price


From the inheritance about UIBarButtonItem and UIButton:

UIBarButtonItem->UIBarItem->NSObject

UIButton->UIControl->UIView->UIResponder->NSObject

you can see, there is not possible to use UIBarButtonSystemItem on UIButton, UIBarButtonItem and UIButton only commonly inherited from NSObject.

like image 33
simalone Avatar answered Nov 06 '22 18:11

simalone