How to change UINavigation Back Button to an image?

江枫思渺然 提交于 2019-12-12 09:00:09

问题


So I have these images that are used for a custom Nav bar and items, they look like this.Currently I have the custom nav bar set up, however I don't know how change the default "back" button to the back button image below, this is what I want to know? Any help would be greatly appreciated. Note: I am using storyboards. Thanks

This is the nav bar:

This is my back button that i don't know how to imlpement.


回答1:


If you want add back button to UINavigation try this:

First create back button:

1. If you don`t need back action:

        UIBarButtonItem *myBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
        style:UIBarButtonItemStyleDone target:nil action:nil];

2. If you want create back button with image try this:

    UIBarButtonItem *myBackButton = [[UIBarButtonItem alloc] initWithImage:[UIImage
    imageNamed:@"yourImageName"] style:UIBarButtonItemStylePlain target:nil
    action:nil]

3. If you want create back button with image and action try this:

    UIBarButtonItem *myBackButton = [[UIBarButtonItem alloc] initWithImage:[UIImage
    imageNamed:@"yourImageName"] style:UIBarButtonItemStylePlain target:self
    action:@selector(goBack:)]

    - (void)goBack:(id)sender {
        [self.navigationController popViewControllerAnimated:YES];
    }

After created back button add its to NavigationBar:

self.navigationItem.backBarButtonItem = myBackButton;

Add back button code you can implement in viewDidLoad method:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.backBarButtonItem = myBackButton;
}

And you need more information see this: UIBarButtonItem Class Reference

Here more examples to you:

  • How to Customize Navigation Bar and Back Button
  • UINavigationController with a Custom Back-Button (Image)
  • Custom back button with a Navigation Controller

  • how to create back button in navigation bar
  • Customize the Back Button of UINavigationItem in the Navigation Bar
  • UINavigationBar Back Button with images



回答2:


Here is how you can do this:

UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target:target action:action;]
self.navigationItem.rightButtonItem = back;

For more info about UIBarButtonItem, go here.

Also try to search next time for similar questions before posting new.



来源:https://stackoverflow.com/questions/16240168/how-to-change-uinavigation-back-button-to-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!