gpt4 book ai didi

iphone - 使用[UINavigationBar外观]更改后退按钮图像

转载 作者:行者123 更新时间:2023-12-03 20:17:42 26 4
gpt4 key购买 nike

我想更改 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];

但它会拉伸(stretch)图像并在其上绘制文本,这是我不想要的。我只想要每个 View 中大小相同的图像。

谢谢!

最佳答案

从 iOS 5.0 开始,您可以使用全局外观修饰符来更改整个应用中的所有后退按钮:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"back_button_bg"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];

背景图像必须是可调整大小的图像才能获得良好的效果。

关于iphone - 使用[UINavigationBar外观]更改后退按钮图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10275724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com