自定义UINavigationItem上的标题未显示

时间:2011-10-27 05:41:48

标签: iphone objective-c xcode uinavigationitem

我正在使用自定义背景图片和标题创建自定义UINavigationItem。显示图像但标题不显示。可能有什么不对? 我是这样做的:

UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 
[button setBackgroundImage:[UIImage imageNamed:@"bar_button_black.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:@"bar_button_black.png"] forState:UIControlStateHighlighted];
[button setTitle:@"Cancel" forState:UIControlStateNormal];
[button setTitle:@"Cancel" forState:UIControlStateSelected];
[button setTitle:@"Cancel" forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(closeButtonClicked) forControlEvents:UIControlEventTouchUpInside]; 

[button setFrame:CGRectMake(0, 0, 49, 30)]; 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

4 个答案:

答案 0 :(得分:0)

我认为你必须删除:

[button setImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 

之后,标题将出现

答案 1 :(得分:0)

删除此行

[button setImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 

或者您可以将标题颜色更改为其他颜色。我希望它能解决您的问题。

将其制作成DrakgrayColor或其他可以克服图像颜色的颜色。

答案 2 :(得分:0)

行。找到了解决方法,但我不确定是否允许:

UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close",@"Close") style:UIBarButtonItemStylePlain target:self action:@selector(closeButtonClicked)];
[self.navigationItem setLeftBarButtonItem:closeButton];

for (UIView *view in self.navigationController.navigationBar.subviews) {        
    if ([[[view class] description] isEqualToString:@"UINavigationButton"]) {
        UINavigationButton *button = {(UINavigationButton *)view};
        [button setBackgroundImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"bar_button_black.png"] forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage imageNamed:@"bar_button_black.png"] forState:UIControlStateHighlighted];

    }
}

答案 3 :(得分:0)

您需要将图像设置为backgroundImage而不是直接。如果你设置这样的图像

[button setImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 

标题不会显示

像这样设置

[button setBackgroundImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal];