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

标签 ios cocoa-touch uibarbuttonitem uitoolbar

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

我想要一些不完全遵循 HIG 的东西。

基本上,我想要一个实心圆形且平坦的彩色UIButton,并带有文本。 (但是,出于显而易见的原因,我不希望文本与图像分开)。

这能实现吗?

你能绘制一个 .cornerradius = 5.0 的 UIBarButtonItem 吗?我相信那只是 UIButton

或者您可以使用蒙版 png 图像作为 UIBarButtonItem,应用色调颜色来更改图像蒙版,并在顶部覆盖一些文本?

或者您是否必须伪造/模仿创建一个UIToolbar,然后在View底部创建一个44像素的lightGreyUIView

如何实现这一点,是否会因违反 HIG 而被拒绝?

最佳答案

您始终可以使用 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];
}

此处更新屏幕

enter image description here

关于ios - 有没有办法在反对 HOG 的工具栏中使用自定义 UIBarButtonItem?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30251793/

相关文章:

ios - 对象动画 - c4framework

iphone - 以动态方式将数组分成多个?对象

iphone - 让 UITableView 转到顶行

objective-c - 套接字通信: has anyone zeromq lib working in your iOS project?

iPhone URL 请求 : Add value to HTTP header field

iphone - NSNotification 是否保留对象?

iphone - 我如何才能将显示您所在页面的三个点放在我的 View 中?

ios - 返回的 UIBarButtonItem 没有出现?

ios - UIBarButtonItem 如何禁用辅助功能 (iOS)

iOS:UISplitViewController 后退按钮设置查询