有没有办法在反对 HOG 的工具栏中使用自定义 UIBarButtonItem?

Is there a way to have custom UIBarButtonItems in a Toolbar that go against the HIG?

这些是 UIToolBar 图标和栏按钮图标的人机界面指南。

我想要一些不完全符合 HIG 的东西。

基本上我想要一个纯色的圆形和扁平的颜色 UIButton,文本在上面。 (但是,出于明显的原因,我不希望文本与图像分开)。

这能实现吗?

你能用 .cornerradius = 5.0 画一个 UIBarButtonItem 吗?我相信那只是UIButton

或者您可以使用蒙版 png 图像作为 UIBarButtonItem,应用 tintcolor 来更改图像蒙版,并在顶部叠加一些文本吗?

或者您是否必须 fake/mimic 创建一个 UIToolbar,然后在 View 的底部创建一个 44 像素的浅灰色UIView

如何实现,会不会因为违反HIG而被拒绝?

您可以使用条形按钮的 initWithCustomView: 初始化程序。

您始终可以使用 UIToolBar 并在其中添加 UIBarButtonItem。由于 UIBarButtonItem 是从 UIBarItem 继承的,然后是 NSObject,您需要修改的功能很少。

使用以下代码在 UIToolBar 中启动您的项目 这样你就可以将自定义 UIButton 作为 UIToolBarItem.

要更改不同状态的按钮背景颜色,您可能需要在数组中按住按钮状态切换,并根据状态更改颜色

这是新的代码:

UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,55, self.view.bounds.size.width, 55)];
toolbar.tintColor = [UIColor redColor];
[self.view addSubview:toolbar];

NSMutableArray *tempArray = [[NSMutableArray alloc]init];
buttonStateArray = [[NSMutableArray alloc]init];
for(int i =0; i < 3; i++){
    UIButton *tempButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    tempButton.backgroundColor = [UIColor redColor];
    tempButton.layer.cornerRadius = 8;
    tempButton.layer.borderWidth = 2;
    tempButton.tag = i;
    tempButton.layer.borderColor = [UIColor blackColor].CGColor;
    [tempButton setTitle:[NSString stringWithFormat:@"Hello %d", i] forState:UIControlStateNormal];
    [tempButton addTarget:self action:@selector(displayTapped:) forControlEvents:UIControlEventTouchUpInside];
    [tempArray addObject:[[UIBarButtonItem alloc]initWithCustomView:tempButton]];

    [buttonStateArray addObject:[NSNumber numberWithBool:NO]];
}

[toolbar setItems:tempArray];

现在按钮的目标方法

-(void)displayTapped:(UIButton *)sender{
BOOL state = [[buttonStateArray objectAtIndex:sender.tag] boolValue];
[buttonStateArray replaceObjectAtIndex:sender.tag withObject:[NSNumber numberWithBool:!state]];
if(!state){
    sender.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
}else {
    sender.backgroundColor = [UIColor redColor];
}

}

此处更新屏幕