UIBarButtonItem有颜色吗?

4th*_*ace 49 iphone cocoa-touch

是否有可能有一个红色的UIBarButtonItem?

Sco*_*rie 63

如果有人正在寻找完全复制简单UIBarButtonItem的代码:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
[button setTitle:@"Delete" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
[button.layer setCornerRadius:4.0f];
[button.layer setMasksToBounds:YES];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor: [[UIColor grayColor] CGColor]];
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0);
[button addTarget:self action:@selector(batchDelete) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem* deleteItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Run Code Online (Sandbox Code Playgroud)

而delete.png是:

delete.png

  • 另外需要注意的是:您需要将QuartzCore库添加到项目中并将其导入到类中以使用CALayer方法. (8认同)
  • 要保存其他人不得不查找它:#import <QuartzCore/QuartzCore.h> (6认同)
  • 我认为默认的角半径可能是5.0f而不是4.0f.此外,人们也可能更喜欢使用1像素宽的图像来保持图像小.这篇文章很有用,谢谢! (2认同)

fri*_*nkr 36

为什么不呢:

[[self editButtonItem] setTintColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)

(在sdk 4.*,ios 5及以上)


小智 26

您可以使用所需的外观创建自定义UIButton,并将其作为自定义视图初始化为UIBarButtonItem.

UIButton *button = [UIButton buttonWithType:....];
...(customize your button)...

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
Run Code Online (Sandbox Code Playgroud)


Ran*_*all 7

您可以在iOS 5中设置UIBarButtonItem的tintColor属性.如果您需要支持iOS 4,请查看此博客文章.它详细介绍了使用UISegmentedControl样式看起来像一个按钮.