我正在尝试将图标(并保留标题)添加到导航控制器的后退按钮。看来如果我设置了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;
但是Apple文档说backBarButtonItem会忽略自定义视图,所以这不起作用。
我也试过这个:
self.navigationItem.leftBarButtonItem = backButton;
self.navigationItem.hidesBackButton = YES;
但是leftBarButtonItem太快显示一个屏幕,而hidesBackButton似乎没有隐藏backBarButtonItem。
还有另一种方法可以将图像和标题同时放到导航backBarButtonItem上吗?
答案 0 :(得分: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;