iOS 7 中 UINavigation 后退按钮的自定义图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18912638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom image for UINavigation Back Button in iOS 7
提问by Majid
I have a custom UIBarButtonItem
with an image that works fine in iOS 6.1. But iOS 7 has a tintColor
and it overlays this color over my image. If I set the tintColor
to [UIColor clearColor]
the button doesn't show up all together.
我有一个自定义UIBarButtonItem
图像,可以在 iOS 6.1 中正常工作。但是 iOS 7 有一个tintColor
,它在我的图像上覆盖了这个颜色。如果我设置tintColor
为[UIColor clearColor]
按钮不会一起显示。
How can I have my back button show up in iOS 7 as it does in iOS 6? Please help?
如何让我的后退按钮像在 iOS 6 中一样显示在 iOS 7 中?请帮忙?
采纳答案by Nitin Gohel
Try to set UIBarButtonItem
like this way in ios7:-
尝试UIBarButtonItem
在 ios7 中这样设置:-
UIImage *temp = [[UIImage imageNamed:@"theImage"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:temp style:UIBarButtonItemStyleBordered target:self action:@selector(action)];
Here is an original post in apple Dev center discussion Forums
这是苹果开发中心讨论论坛中的原始帖子
For supporting both version iOS7 as well as lower then you check system-version
and set code like:-
为了同时支持 iOS7 和更低版本,您可以检查system-version
并设置如下代码:-
UIImage *temp=nil;
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
temp = [UIImage imageNamed:@"btn-back.png"];
}
else
{
temp = [[UIImage imageNamed:@"btn-back.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
}
回答by Johannes
You should use appearance on UINavigationBar to globally set the custom back button.
您应该使用 UINavigationBar 上的外观来全局设置自定义后退按钮。
[UINavigationBar appearance].backIndicatorImage = customBackButton;
[UINavigationBar appearance].backIndicatorTransitionMaskImage = customBackButton;
回答by learner
The following seems to make a bit more sense for people who don't want to mess with the existing target action, etc. Just copy and paste. Also this forces iOS to use yourimage with all of its flairs -- as opposed to simply using a template/impression of the image.
对于不想弄乱现有目标操作等的人来说,以下内容似乎更有意义。只需复制和粘贴即可。这也迫使 iOS 使用您的图像及其所有风格——而不是简单地使用图像的模板/印象。
- (void)setCustomNavigationBackButton
{
UIImage *backBtn = [UIImage imageNamed:@"arrow"];
backBtn = [backBtn imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationItem.backBarButtonItem.title=@"";
self.navigationController.navigationBar.backIndicatorImage = backBtn;
self.navigationController.navigationBar.backIndicatorTransitionMaskImage = backBtn;
}
arrow
is the name of your image.
arrow
是您的图像的名称。
回答by Bill Chan
swift version:
快速版本:
var backBtn = UIImage(named: "return_menu")
backBtn = backBtn?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
self.navigationController!.navigationBar.backIndicatorImage = backBtn;
self.navigationController!.navigationBar.backIndicatorTransitionMaskImage = backBtn;
回答by Rob Phillips
Try it this way:
试试这个方法:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationController.navigationBar.backIndicatorImage = [UIImage imageNamed:@"yourImageName.png"];
self.navigationController.navigationBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"yourImageName.png"];
This will create an image mask in the global tint colour which will give you your own custom icon. Does not work for colour images.
这将创建一个全局色调颜色的图像蒙版,这将为您提供您自己的自定义图标。不适用于彩色图像。
回答by Funny
// ADDING IMAGE TO BUTTON
// 将图像添加到按钮
UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
[refreshButton setFrame:CGRectMake(0,0,30,30)];
refreshButton.userInteractionEnabled = YES;
[refreshButton setImage:[UIImage imageNamed:@"yourimage.jpg"] forState:UIControlStateNormal];
// ASSIGNING THE BUTTON WITH IMAGE TO BACK BAR BUTTON
UIBarButtonItem *refreshBarButton = [[[UIBarButtonItem alloc] initWithCustomView:refreshButton] autorelease];
self.navigationItem.leftBarButtonItem = refreshBarButton;