简体   繁体   English

导航栏后退按钮上的图像

[英]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 : 您可以通过将UINavigationBar子类化,然后创建如下方法来实现:

+ (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. ps:我更喜欢使用单例,这就是它的原因。

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. 在视图外部的nib文件中添加一个后退按钮。 customize it as you wish. 根据需要自定义它。
  2. connect it as an IBOutlet. 将其作为IBOutlet连接。 Let's call it btnBack. 我们称它为btnBack。
  3. in ViewDidLoad do this: 在ViewDidLoad中执行以下操作:

     UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:btnBack]; [self.navigationItem setLeftBarButtonItem:back animated:YES]; 
  4. connect an action to your backBtn like this: 像这样将一个动作连接到您的backBtn:

     - (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]'. 或者,对于特定的导航栏,请指定您的navBar而不是“ [UINavigationBar外观]”。 This works perfectly. 这很完美。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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