简体   繁体   English

普遍设置自定义后退栏按钮,没有标题

[英]set custom back bar button universally with no title

I want to set custom back bar button for all controllers in the app. 我想为应用程序中的所有控制器设置自定义后退栏按钮。 I tried using this: 我试过用这个:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

It sets the image. 它设置图像。 That's OK. 没关系。 But, what i really want to do is that, i just want a custom button for back bar button which does not contain any title etc. The code above works, but it adds automated titles and resizes the back bar button item. 但是,我真正想做的是,我只想要一个自定义按钮用于后栏按钮,它不包含任何标题等。上面的代码可以工作,但它会添加自动标题并调整后栏按钮项的大小。 My need is to have a fixed frame, no-title back bar button item for all controllers in the app. 我需要为应用程序中的所有控制器设置一个固定的框架,无标题后退栏按钮项。

I've resolved it. 我已经解决了。 Just make a category over UIViewController and import it in prefix.pch file. 只需在UIViewController上创建一个类别,然后将其导入prefix.pch文件即可。 Then write a method: customViewWillAppear: and swizzle it with viewWillAppear method: 然后编写一个方法:customViewWillAppear:并使用viewWillAppear方法调用它:

+(void)load{

Method viewWillAppear = class_getInstanceMethod(self, @selector(customViewWillAppear:));

Method customViewWillAppear = class_getInstanceMethod(self, @selector(viewWillAppear:));
method_exchangeImplementations(viewWillAppear, customViewWillAppear);

} }

Add the above method to that category class. 将上述方法添加到该类别类。 Then implement your customViewWillAppear method like this: 然后实现你的customViewWillAppear方法,如下所示:

-(void)customViewWillAppear:(BOOL)animated{
    [self customViewWillAppear:animated];
    if([self.navigationController.viewControllers indexOfObject:self] != 0  && !self.navigationItem.hidesBackButton){
        UIBarButtonItem *cancelBarButton = nil;
        UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelButton addTarget:self action:@selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchUpInside];
        [cancelButton setBackgroundImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
        [cancelButton sizeButtonToFit];

        cancelBarButton = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];

        NSMutableArray * leftButtons = [NSMutableArray arrayWithObject:cancelBarButton];
        [leftButtons addObjectsFromArray:self.navigationItem.leftBarButtonItems];
        [self.navigationItem setLeftBarButtonItem:nil];
        [self.navigationItem setLeftBarButtonItems:leftButtons];
    }

    [self.navigationItem setHidesBackButton:YES];
}
-(void)popViewControllerWithAnimation{
    [self.navigationController popViewControllerAnimated:YES];
}

Now, for every controller in your code, you have a custom back button. 现在,对于代码中的每个控制器,您都有一个自定义后退按钮。 This took me a lot of time to implement and figure out. 这花了我很多时间来实现和弄清楚。 Hope it'll help you guys all too. 希望它也会帮助你们。

EDIT: Please use the following code to support iOS7> back swipe feature; 编辑:请使用以下代码支持iOS7>后滑功能;

UIImage *image = [UIImage imageForName:@"some_image"];
navBar.backIndicatorImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navBar.backIndicatorTransitionMaskImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

Create a base view controller and add the following code; 创建基本视图控制器并添加以下代码;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}

I've had a similar problem before and I've searched everywhere for a solution. 我之前遇到过类似的问题,我到处寻找解决方案。

The only one I've found, which works for your problem was to implement in EVERY view controller a UILeftBarButton which does the popping. 我发现的唯一一个适用于你的问题的是在每个视图控制器中实现一个执行弹出的UILeftBarButton。

You can change the background image the way you're doing, but if you set the text to nil or empty text (""), you're button just won't show up. 您可以按照自己的方式更改背景图像,但如果将文本设置为nil或空文本(“”),则按钮就不会显示。

You also can't change the View of the UIBackBarButton, only it's text (so no custom button). 您也无法更改UIBackBarButton的视图,只能更改它的文本(因此没有自定义按钮)。

我做了什么,使用外观代理将后退按钮标题标签alpha设置为零。

You could write a category like this, 你可以写一个像这样的类别,

//
//  UINavigationItem+BarAditions.h
//
//  Created by Satheeshwaran on on 7/5/13.
//


#import <Foundation/Foundation.h>


@interface UINavigationItem (PersonaAddition)

- (void)setCustomBackButton;

@end


//
//  UINavigationItem+BarAditions.m
//
//  Created by Satheeshwaran on on 7/5/13.
//

#import "UINavigationItem+PersonaAddition.h"


@implementation UINavigationItem (PersonaAddition)

- (void)setCustomBackButton 
{
    //customize ur back button here.
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame=CGRectMake(0, 0, 60, 30);
    [backButton addTarget:target action:@selector(didPressLeftItem:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.leftBarButtonItem = barItem;    

}

and import this category in all ur files and call the setCustomBackButton in all ur classes. 并在所有ur文件中导入此类别,并在所有类中调用setCustomBackButton。 This works fine for me, even in iOS 7. 这对我来说很好,即使在iOS 7中也是如此。

In ViewDidLoad of all ur classes. 在所有你的类的ViewDidLoad中。

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.navigationItem setCustomBackButton];

}

My code in ViewDidLoad: 我在ViewDidLoad:代码ViewDidLoad:

// Set back button image
if ([[self.navigationController viewControllers] objectAtIndex:0] != self) {
    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    btnBack.frame = CGRectMake(0,0,38,30);
    [btnBack setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackButton"] forState:UIControlStateNormal];
    [btnBack addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
    [self.navigationItem setLeftBarButtonItem:leftBtn];
}

try this code: 试试这段代码:

UIButton *tempbtn = [[UIButton alloc] init];
[tempbtn setImage:[UIImage imageNamed:@"testbtn.png"] forState:UIControlStateNormal];
UIBarButtonItem *temp = [[UIBarButtonItem alloc] initWithCustomView:tempbtn];
self.navigationItem.rightBarButtonItem = temp;

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

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