我添加了一个 UIToolbar 实例和其顶部的按钮。每个按钮都属于 UIBarButtonItem 类。
我的要求是每个按钮都有一个自定义的布局,我不想使用苹果提供的原生按钮样式。所以我在 Interface Builder 中有 3 个选项(普通、边框、完成)。
我选择了“平原”样式,并选择了要添加为背景的图像
栏项目 -> 图像。
但它对我来说不起作用,使用普通选项让我更接近一点,因为边框和完成距离更近,但它仍然以白色轮廓显示图像。无论如何,是否添加了按钮上的图像,并且它们看起来完全一样。使用 UIButton 时这是一个非常简单的任务。我只是在 UIButton 的属性检查器中使用了图像选项并选择了我想要的图像。但在这里使用 UIBarButtonitem 时它根本不起作用。
这是它的显示方式
谢谢
泰穆尔
Taimur,
我遇到了和你遇到的同样的问题。我们的设计师为应用程序导航栏中的按钮指定了自定义外观。这是我编写的用于生成我们需要的 UIBarButtonItems 的实用方法,您应该能够根据您的需要修改它:
+ (UIBarButtonItem *)createSquareBarButtonItemWithTitle:(NSString *)t target:(id)tgt action:(SEL)a
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Since the buttons can be any width we use a thin image with a stretchable center point
UIImage *buttonImage = [[UIImage imageNamed:@"SquareButton.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
UIImage *buttonPressedImage = [[UIImage imageNamed:@"SquareButton_pressed.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
[[button titleLabel] setFont:[UIFont boldSystemFontOfSize:12.0]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[button setTitleShadowColor:[UIColor colorWithWhite:1.0 alpha:0.7] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateHighlighted];
[[button titleLabel] setShadowOffset:CGSizeMake(0.0, 1.0)];
CGRect buttonFrame = [button frame];
buttonFrame.size.width = [t sizeWithFont:[UIFont boldSystemFontOfSize:12.0]].width + 24.0;
buttonFrame.size.height = buttonImage.size.height;
[button setFrame:buttonFrame];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];
[button setTitle:t forState:UIControlStateNormal];
[button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
return [buttonItem autorelease];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)