简体   繁体   中英

Image on Navigation Bar Back Button

In our application I use Left and right button on navigation bar. I want a image on back (left)button.If i use segment for that then necessary to define a action for back button. Please advice me for any method.

You can do it by subclassing UINavigationBar, then create a method like this :

+ (UIBarButtonItem *)barButtonWithImage:(NSString *)imageName target:(id)target action:(SEL)action {
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
     [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
     UIImage *titleImage = [UIImage imageNamed:imageName];
     [button setImage:titleImage forState:UIControlStateNormal];
     [button sizeToFit];
     UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    return item;
}

And then add it to your navigation bar like this :

    [self.navigationItem setLeftBarButtonItem:[TFNavigationBar barButtonWithImage:@"rp_settings.png" target:self action:@selector(showSettings)]];

ps: I prefer to use singletons, that's why that it is.

The easiest way we found to replace the back button with our own goes like this.

  1. Add a back button in the nib file outside your view. customize it as you wish.
  2. connect it as an IBOutlet. Let's call it btnBack.
  3. in ViewDidLoad do this:

     UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:btnBack]; [self.navigationItem setLeftBarButtonItem:back animated:YES]; 
  4. connect an action to your backBtn like this:

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

And thats it you got yourself a customized back button :)

是的,如果您使用Bar Button项,则可以转到Interface Builder中的Bar Button项属性,然后应该可以选择Image选项。

If you want to simply replace your back button with an image through out your app..

[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back_button"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back_button"]];

Or for specific navigation bars specify your navBar instead of '[UINavigationBar appearance]'. This works perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM