我想更改 UINavigationBar 的后退按钮
我可以使用以下代码来做到这一点:
// 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 方法中。我想为整个程序做一次。
我的尝试是这样的:
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];
但它会拉伸图像并在其上绘制文本,这是我不想要的。我只想要每个视图中大小相同的图像。
谢谢!