我想为每个导航项目(按钮)设置背景图像(通用)。
这些图像应该如何显示?
是否有固定的尺寸或是否可以创建重复的背景图像?
如何以编程方式更改此背景图像?
感谢您的帮助
更新:
我发现此代码用于更改此按钮的背景,但它无效。
UIImage *barButton = [UIImage imageNamed:@"button_background.png"];
[[UIBarButtonItem appearance] setBackgroundImage:barButton forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
我不知道应该是哪个尺寸。
P.S。我想改变按钮的背景就像你可以改变背景颜色一样(self.navigation.controller.nvigationBar.tintColor ..)
答案 0 :(得分:2)
以下代码可能会解决您的问题
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:@"button_background.png"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 30);
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = cancelButton;
答案 1 :(得分:0)
检查此代码
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:@"yourImage.png"]
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
让我知道它是否有效
干杯
答案 2 :(得分:0)
类别怎么样? 例如,您可以这样做:
@interface UIBarButtonItem(JFAdditions)
- (id)initWithTitle:(NSString *)title image:(UIImage*)img;//add target and action if need
@end
@implementation UIBarButtonItem(JFAdditions)
- (id)initWithTitle:(NSString *)title image:(UIImage*)img
{
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
//add background image and text on button, target and action if need, also
self = [self initWithCustomView:btn];
return self;
}