0

我正在尝试向导航控制器的后退按钮添加一个图标(并保留标题)。似乎如果我设置 UIBarButtonItem 的图像它会隐藏标题,所以我想我会尝试自定义视图。我试过了

UIButton* customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
[customButton setTitle:@"Title" forState:UIControlStateNormal];
[customButton setAdjustsImageWhenHighlighted:YES];
[customButton setFrame:CGRectMake(0, 0, 125, 32)];

UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.backBarButtonItem = backButton;

但苹果文档说 backBarButtonItem 忽略自定义视图,所以这不起作用。

我也试过这个:

self.navigationItem.leftBarButtonItem = backButton;
self.navigationItem.hidesBackButton = YES;

但是 leftBarButtonItem 过早显示一个屏幕, hidesBackButton 似乎没有隐藏 backBarButtonItem。

有没有另一种方法可以将图像和标题都放到导航 backBarButtonItem 上?

4

1 回答 1

1

首先隐藏使用UINavigationController代码提供的后退按钮self.navigationItem.hidesBackButton = YES;

并使用以下代码设置带有图像和标题的自己的按钮

UIButton* customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
[customButton setTitle:@"Title" forState:UIControlStateNormal];
[customButton setAdjustsImageWhenHighlighted:YES];
[customButton setFrame:CGRectMake(0, 0, 125, 32)];

UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.backBarButtonItem = backButton;
于 2013-04-05T14:28:23.643 回答