我可以为UIButton使用UIBarButtonSystemItem吗?

7
我正在尝试自定义一个UIButton,我想使用一个UIBarButtonSystemItem样式,这个样式可用于UIBarButtonItem对象。
如何在UIButton对象上使用UIBarButtonSystemItem样式?

你可以通过这个提取器(https://github.com/0xced/iOS-Artwork-Extractor)找到任何按钮的系统图像,并将其用于你的按钮。 - Mike
4个回答

8

你不能这么做。UIBarButton和UIButton是两个完全不同的类。最好的办法是找到与你所需内容相似的图片并将其添加到UIButtons中。


4

使用一个包含UIBarButtonItems的UIToolbar的UIView,而不是UIButton。

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

2

关于UIBarButtonItem和UIButton的继承关系:

UIBarButtonItem->UIBarItem->NSObject

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

可以看出,不能在UIButton上使用UIBarButtonSystemItem,UIBarButtonItem和UIButton只有共同继承自NSObject。


0

你只能在工具栏或导航栏上使用它们。

一个解决方法/技巧是在想放置UIBarButton的地方使用UIToolbars。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接