普遍设置自定义后退栏按钮,没有标题 [英] set custom back bar button universally with no title

查看:139
本文介绍了普遍设置自定义后退栏按钮,没有标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为应用中的所有控制器设置自定义后退栏按钮。我尝试使用这个:

I want to set custom back bar button for all controllers in the app. I tried using this:

[[UIBarButtonItem外观]
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.

推荐答案

我'我解决了它。只需在UIViewController上创建一个类别,然后将其导入prefix.pch文件即可。然后编写一个方法:customViewWillAppear:并使用viewWillAppear方法调用它:

I've resolved it. Just make a category over UIViewController and import it in prefix.pch file. Then write a method: customViewWillAppear: and swizzle it with viewWillAppear method:

+(void)load{

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

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

}

将上述方法添加到那个类别。然后实现你的customViewWillAppear方法,如下所示:

Add the above method to that category class. Then implement your customViewWillAppear method like this:

-(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.

编辑:
请使用以下代码支持iOS7>后滑功能;

Please use the following code to support iOS7> back swipe feature;

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];
}

这篇关于普遍设置自定义后退栏按钮,没有标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆