所以我正在尝试创建一个自定义的UIBarButtonItem,但我不能为我的生活弄清楚为什么标题不会显示。也许其他人可以发现它:\
以下是我用来创建栏按钮的代码:
+ (UIBarButtonItem *)barButtonItemWithTitle:(NSString *)title normal:(UIImage *)normal highlighted:(UIImage *)highlight selected:(UIImage *)selected target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0.0f, 0.0f, normal.size.width, normal.size.height)];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setImage:normal forState:UIControlStateNormal];
[button setImage:selected forState:UIControlStateHighlighted];
[button setImage:selected forState:UIControlStateSelected];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:@"Cancel"
normal:[UIImage imageNamed:@"back_button_active.png"]
highlighted:[UIImage imageNamed:@"back_button_highlight.png"]
selected:[UIImage imageNamed:@"back_button_selected.png"]
target:self
action:@selector(popViewController)];
任何帮助将不胜感激!
P.S。这是在iOS 5中使用ARC。
答案 0 :(得分:1)
标题和图像是UIButton中的互斥属性。如果你想拥有自定义背景的标题,那么你应该使用backgroundImage属性:
...
[button setBackgroundImage:normal forState:UIControlStateNormal];
[button setBackgroundImage:selected forState:UIControlStateHighlighted];
[button setBackgroundImage:selected forState:UIControlStateSelected];
...