问题描述
导航到已实施导航控制器的应用中的任何视图时,它会显示一个后退按钮以转到上一个视图。有没有办法可以使用自定义图像而不是默认图像?
On navigating to any view in an app which has navigation controller implemented, it shows a back button to go to the previous view. Is there a way I can use custom image instead of the default one?
推荐答案
是的,你可以。
您可以通过设置其导航项的后退栏按钮项来控制在给定视图控制器上按下另一个视图控制器时标准后退按钮的外观(您可以自定义标题或使用图片):
You can control how the standard back button will look when another view controller is pushed on top of a given view controller by setting its navigation item's back bar button item (you can customize the title or use an image):
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = btn;
[btn release];
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:yourImage style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = btn;
[btn release];
注意:您可以在父视图控制器中配置它将其他视图控制器推到它上面。配置在父中完成,当某些视图控制器位于顶部时,后退按钮的外观会发生变化。点击后退按钮可以按预期返回。
Note: you configure this in a "parent" view controller that may have other view controller(s) pushed on top of it. The configuration is done in the "parent" and the appearance of the back button is changed when some view controller is on top. Tapping the back button brings you back as expected.
您还可以创建自己的UIBarButtonItem并将其设置为leftButtonItem on当前视图控制器上的导航栏:
You can also create your own UIBarButtonItem and set it as the leftButtonItem on the navigation bar on the current view controller:
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:yourImage style:UIBarButtonItemStyleBordered target:self action:@selector(yourMethod:)];
self.navigationItem.leftBarButtonItem = btn;
[btn release];
注意:在这种情况下,后退/左侧栏按钮项是已更改为当前视图控制器(当它在顶部时)。您必须实现 yourMethod:
方法。如果你只想让按钮返回,你必须通过调用 [self.navigationController popViewControllerAnimated:YES];
。
Note: in this case, the back / left bar button item is changed for the current view controller (when it is on top). You must implement the yourMethod:
method. If you simply want the button to go back, you have to handle popping the view controller yourself by calling [self.navigationController popViewControllerAnimated:YES];
.
这篇关于如何在导航栏后退按钮中显示自定义图像,而不是导航栏本身显示的默认按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!