更改自定义导航栏项目的文本

时间:2021-08-08 22:59:15

I am creating a custom UINavigationBar BarButtonItem as follows:

我正在创建一个自定义UINavigationBar BarButtonItem,如下所示:

 // Set up bar button items
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35.0f, 35.0f)];
    [backButton setImage:[UIImage imageNamed:@"myimg.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(myselector) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

This is working perfectly, but when I go to set the title of the UIButton or BarButtonItem, it won't show up. Any suggestions?

这是完美的工作,但当我去设置UIButton或BarButtonItem的标题时,它将不会显示。有什么建议?

Tried...

    [backButton setTitle:@"Edit" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

And...

     self.navigationItem.leftBarButtonItem.title = @"Edit";

without luck.

2 个解决方案

#1


3  

The problem is that image and title use same viewspace for display.

问题是图像和标题使用相同的视图空间进行显示。

So when we use image with title first it show image and than title (right side of image).

因此,当我们首先使用带有标题的图像时,它会显示图像而不是标题(图像的右侧)。

If size of UIButton is same as image than we can't see Title.

如果UIButton的大小与图像相同,则我们看不到标题。

A simple solution is set image in background. So try this:

一个简单的解决方案是在后台设置图像。所以试试这个:

[backButton setBackgroundImage:[UIImage imageNamed:@"myimg.png"] forState:UIControlStateNormal];

#2


0  

UIImage *image = [UIImage imageNamed:@"myimg.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:image forState:UIControlStateNormal];
[backButton setTitle:@"Edit" forState:UIControlStateNormal];
 [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]
backButton.frame=CGRectMake(0, 0, 35.0f, 35.0f);

UIBarButtonItem *barButton= [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
self.toolbar.items = [NSArray arrayWithObject:barButton];

#1


3  

The problem is that image and title use same viewspace for display.

问题是图像和标题使用相同的视图空间进行显示。

So when we use image with title first it show image and than title (right side of image).

因此,当我们首先使用带有标题的图像时,它会显示图像而不是标题(图像的右侧)。

If size of UIButton is same as image than we can't see Title.

如果UIButton的大小与图像相同,则我们看不到标题。

A simple solution is set image in background. So try this:

一个简单的解决方案是在后台设置图像。所以试试这个:

[backButton setBackgroundImage:[UIImage imageNamed:@"myimg.png"] forState:UIControlStateNormal];

#2


0  

UIImage *image = [UIImage imageNamed:@"myimg.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:image forState:UIControlStateNormal];
[backButton setTitle:@"Edit" forState:UIControlStateNormal];
 [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]
backButton.frame=CGRectMake(0, 0, 35.0f, 35.0f);

UIBarButtonItem *barButton= [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
self.toolbar.items = [NSArray arrayWithObject:barButton];