Can I use UIBarButtonSystemItem for UIButton?

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

How to use UIBarButtonSystemItem style for a UIButton object?

+7
ios cocoa-touch uibutton uibarbuttonitem
source share
4 answers

You can not. UIBarButton and UIButton are two completely different classes. The best you can do is find the images next to what you are looking for and add them to UIButtons.

+8
source share

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"); } 
+4
source share

From inheritance about UIBarButtonItem and UIButton:

UIBarButtonItem-> UIBarItem-> NSObject

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

you can see that you cannot use the UIBarButtonSystemItem for UIButton, UIBarButtonItem, and UIButton, which are usually inherited from NSObject.

+2
source share

you can use them only on the toolbar in the navigation bar.

workaround / hack can be used by UIToolbars where you want to place UIBarButtons

0
source share

All Articles