Search code examples
objective-cxcodenavbar

How to change UINavigation Back Button to an image?


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:

enter image description here

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

enter image description here


Solution

  • If you want add back button to UINavigation try this:

    First create back button:

    1. If you don`t need to back action:

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

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

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

    3. If you want to create back button with image and action then 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 it to NavigationBar:

    self.navigationItem.backBarButtonItem = myBackButton;
    

    Implementation of logic "Add back button" you can put in viewDidLoad method:

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

    And you need to more information see this: UIBarButtonItem Class Reference

    Here more examples for you: