使用[UINavigationBar外观]更改后退按钮图像 [英] use [UINavigationBar appearance] to change back button image
本文介绍了使用[UINavigationBar外观]更改后退按钮图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想更改UINavigationBar的后退按钮
I'd like to change the back button of my UINavigationBar
我可以使用以下代码进行操作:
I can do it using this code:
// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:@"backag.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"selback.png"] forState:UIControlStateHighlighted];
button.adjustsImageWhenDisabled = NO;
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 30, 30);
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
self.navigationItem.hidesBackButton = YES;
// Cleanup
[customBarItem release];
但是我必须将该代码放入每个viewDidLoad方法中.我想为整个程序做一次.
but I have to put that code in each viewDidLoad method. I'd like to do it once for the whole program.
我的尝试是这样
UIImage *buttonBack30 = [[UIImage imageNamed:@"backag"] //16 5
resizableImageWithCapInsets:UIEdgeInsetsMake(1000, 1000, 1000, 1000)];
UIImage *buttonBack31 = [[UIImage imageNamed:@"selback"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack31
// forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack31
forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:[UIColor colorWithRed:(163.0f/255.0f) green:(0.0f) blue:(0.0f) alpha:1.0f] forKey:UITextAttributeTextColor];
[attributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
// [attributes setValue:[UIFont fontWithName:@"Helvetica" size:15] forKey:UITextAttributeFont];
[attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateHighlighted];
但是它会拉伸图像并在其上绘制文本,这是我不想要的.我只希望每个视图中的图像大小相同.
but it stretches out the image and draws text over it, which I don't want. I just want an image that is the same size in each view.
谢谢!
推荐答案
这可能会解决.在顶部的ur appdelegate中添加以下内容.
This might work out. Add the below in ur appdelegate in the top.
@implementation UIViewController (CustomFeatures)
-(void)setNavigationBar{
// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:@"backag.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"selback.png"] forState:UIControlStateHighlighted];
button.adjustsImageWhenDisabled = NO;
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 30, 30);
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
self.navigationItem.hidesBackButton = YES;
// Cleanup
[customBarItem release];
}
@end
,然后在您的viewDidLoad
这篇关于使用[UINavigationBar外观]更改后退按钮图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文