Can I use a UIBarButtonSystemItem for a UIButton?

落爺英雄遲暮 提交于 2020-01-11 08:34:09

问题


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?


回答1:


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.




回答2:


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");
}



回答3:


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.




回答4:


you can only use them on a toolbar on navbar.

a workaround/hack could be use UIToolbars where you want UIBarButtons to be placed



来源:https://stackoverflow.com/questions/21360545/can-i-use-a-uibarbuttonsystemitem-for-a-uibutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!