【问题标题】:Can I use a UIBarButtonSystemItem for a UIButton?我可以为 UIButton 使用 UIBarButtonSystemItem 吗?
【发布时间】:2014-02-17 02:13:07
【问题描述】:
我正在尝试自定义UIButton,并且我想使用可用于UIBarButtonItem 对象的UIBarButtonSystemItem 样式。
如何在UIBUtton 对象上使用UIBarButtonSystemItem 样式?
【问题讨论】:
标签:
ios
cocoa-touch
uibutton
uibarbuttonitem
【解决方案1】:
使用 UIView 而不是包含 UIToolbar 和 UIBarButtonItems 的 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】:
您只能在导航栏的工具栏上使用它们。
一种解决方法/黑客可以使用 UIToolbars 您希望放置 UIBarButtons 的位置
【解决方案3】:
从关于 UIBarButtonItem 和 UIButton 的继承:
UIBarButtonItem->UIBarItem->NSObject
UIButton->UIControl->UIView->UIResponder->NSObject
你可以看到,UIBarButtonSystemItem 是不可能在 UIButton 上使用的,UIBarButtonItem 和 UIButton 通常只继承自 NSObject。
【解决方案4】:
你不能。 UIBarButton 和 UIButton 是两个完全不同的类。您能做的最好的事情就是找到与您正在寻找的图像接近的图像并将它们添加到 UIButtons。