自定义 UINavigationItem 上的标题未显示

发布于 2024-12-12 10:05:53 字数 904 浏览 2 评论 0原文

我正在使用自定义背景图像和标题创建自定义 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];

I'm creating custom UINavigationItem with custom background image and title. The image is shown but the title doesn't. What could be wrong?
I'm doing it this way:

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慕烟庭风 2024-12-19 10:05:53

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

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

标题将不会显示

这样设置

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

You need to set image as backgroundImage not direct. If you set image like this

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

the title will not be displayed

set like this

[button setBackgroundImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 
仙女 2024-12-19 10:05:53

我认为你必须删除:

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

之后,标题就会出现

I think you must remove:

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

After that, the title will appear

年华零落成诗 2024-12-19 10:05:53

删除此行

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

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

将其设置为 DrakgrayColor 或其他可以克服图像颜色的颜色。

Either remove this line

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

or you can change the Title color to some other color.i hope it will solve your problem.

make it to DrakgrayColor or some other color which can overcome your image color.

你げ笑在眉眼 2024-12-19 10:05:53

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

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];

    }
}

OK. Found a workaround, however I'm not sure whether it is allowed:

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];

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文