iOS 5:如何实现自定义(图像)后退按钮 [英] iOS 5: How to implement a custom (image) back button

查看:108
本文介绍了iOS 5:如何实现自定义(图像)后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的博客上完成了教程,所以我知道如何制作一个可以显示底部(堆栈)viewcontroller标题的可伸缩按钮。但我希望做的是有图标(如家的房子),没有文字,也没有调整大小。

I've done the tutorial at my blog, so I know how to make a stretchable button that can display the bottom (stack) viewcontroller's title. But what I was hoping to do is have icons (like a house for HOME) and no text and not resize.

使用我的自定义图像和下面的代码,我得到一个拉伸版本(不想要),标题超过顶部(不想要),点击时显示/突出显示(很好);

Using my custom image and this code below, I get a stretched version (not wanted) with title over top (not wanted) and it does tint/highlight when clicked (is good);

UIImage *backButtonImage = [UIImage imageNamed:@"backButton_30.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

屏幕截图1

现在,我在这里搜索并阅读所有返回旧答案的类似问题,并且对我有奇怪的结果。这是我试过的代码;

Now, I've searched on here and read all the similar questions which return old answers, and have strange results for me. Here is the code I tried;

  UIImage *backButtonImage = [UIImage imageNamed:@"backButton_30.png"];
  UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStyleBordered target:nil action:nil];
  self.navigationItem.backBarButtonItem = backButton;

此方法不会拉伸我的自定义图像按钮(很好),也不会显示文字(我想要的)但是它下面仍然有原始的蓝色按钮(WTF),单击时我的自定义按钮没有色调,只有它下面的蓝色按钮!

This method doesn't stretch out my custom image button (is good), nor does it show text (what I want) however there is still the original blue button under it (WTF), and my custom button doesn't tint when clicked, only the blue button under it does!

屏幕截图2

请帮助,我错过了什么?

Please help, what am I missing?

*更新

我通过使用可调整大小的图片修复了一下。这迫使它不要'伸展'

I've fixed it up a bit by using a resizable image. This forces it not to 'stretch'

UIImage *backButtonHomeImage = [[UIImage imageNamed:@"backButtonHomeWhite_30.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonHomeImage  forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

要修复我必须执行的按钮上显示的标题

To fix the title showing up on the button I had to do

self.title =@" ";

现在这有点肮脏,但似乎有效。现在唯一的问题是我想在不同的视图上使用不同的后退按钮,这种方法会造成一些麻烦;设置按钮的最后一个视图会覆盖所有其他视图。因此,最终,根据您在应用程序中的导航方式,返回上一个视图时会出现错误的后退按钮,并且它永远不会重置为正确的按钮。

Now this is a bit of a dirty fix but it seems to be working. The only problem left now is that I want a different back button on different views, and this method is causing some trouble; the last view that sets the button over-rides all other views. So in the end, depending on how you navigate through the app, returning to a previous view has the wrong back button and it never resets to the correct one.



更新2:潜在想法:


UPDATE 2: POTENTIAL IDEA:

以下是一个合理的解决方案,还是一个容易破坏的黑客?

Would the following be a reasonable solution, or is it a hack that is liable to break something?

隐藏默认后退按钮,就像这样,

Hiding the default back button, like so,

[self.navigationItem setHidesBackButton:YES animated:NO];

...然后使用自定义UIBarButtonItem,我想要放入的样式中的按钮后退按钮的位置,发送 popViewControllerAnimated:消息到UINavigationController。

...and then using a custom UIBarButtonItem, with a button in the style I actually want placed in the location of the back button, that sends a popViewControllerAnimated: message to the UINavigationController when tapped.

如果您知道更强大的解决方案,请分享,谢谢。

If you know of a more robust solution please do share, thank you.

推荐答案

假设你当前的解决方案

UIImage *backButtonHomeImage = [[UIImage imageNamed:@"backButtonHomeWhite_30.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonHomeImage  forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

您可以接受,因此唯一的问题是当您返回时如何更新该按钮的外观在您的视图之间,一种可行的方法是在每个控制器的 viewWillAppear:方法中执行上面的代码:

is acceptable to you, so the only problem left is how to update that button appeareance when you go back and forth between your views, an approach that could work is executing the code above in each of your controllers' viewWillAppear: method:

- (void)viewWillAppear:(BOOL)animated {
    UIImage *backButtonHomeImage = [[UIImage imageNamed:@"backButtonHomeWhite_30.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonHomeImage  forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}

如果您对目前的自定义方式<$ c不满意$ c> UIBarButtonItem ,方法是使用 initWithCustomView:初始化条形按钮项。在这种情况下,您可以使用您喜欢的图像指定例如 UIImageView ,它应该可以工作:

If you are not satisfied with your current approach to having a custom UIBarButtonItem, the way to go is initializing your bar button item with initWithCustomView:. In this case, you can specify, e.g., a UIImageView with the image you like and it should work:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:[UIImageView ...]];

希望这会有所帮助。

这篇关于iOS 5:如何实现自定义(图像)后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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