ios - 无法更改 UIBarButtonItem 的背景

标签 ios objective-c uibarbuttonitem uiappearance

我需要在一个 ViewController 中更改 UIBarButtonItem 的默认(已在我的 AppDelegate 中声明)背景。

所以,在我的 AppDelegate 中我有:

UIImage *navBarItemBackground = [UIImage imageNamed:@"navbar_button_green"];
[[UIBarButtonItem appearance] setBackgroundImage:navBarItemBackground
                                            forState:UIControlStateNormal
                                               style:UIBarButtonItemStyleBordered
                                          barMetrics:UIBarMetricsDefault];

当我需要恢复按钮的默认背景时 - 我在我的 viewController 中实现:

self.navigationItem.rightBarButtonItem = nil;
UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"Сохранить" style:UIBarButtonItemStyleBordered target:self action:@selector(clickOnSend:)];
[rightBarItem setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal style:UIBarButtonItemStyleBordered barMetrics:UIBarMetricsDefault];
self.navigationItem.rightBarButtonItem = rightBarItem;

但我认为将空文件设置为图像是不正确的。有没有更简单的方法来实现这种行为?谢谢!

最佳答案

如果你只想改变一个或几个UIViewController的 UIBarButtonItems backgroundImage,你最好在特定的UIViewController中改变,如后者你使用的情况。而 [[UIBarButtonItem appearance] setBackgroundImage:forState:style:barMetrics:]; 将更改所有 UIBarButtonItems 背景图像,这可能不是更好的。

关于ios - 无法更改 UIBarButtonItem 的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20606082/

相关文章:

ios - Xcode 强制构建到锁定的设备?

ios - 无法在Ionic 3中添加iOS平台

ios - 如何从 UI 按钮创建 UIBarButtonItem 类?

iphone - 使用内部自定义图像创建像素完美的标准 UIBarButtonItem

ios - 屏幕截图中缺少 GPUImage 输出图像

ios - 从内存数据流在 iOS 上播放音频

ios - plist文件中的NSArray或NSDictionary

iphone - 在重新验证 iOS 自动续订订阅时,我应该使用旧的收据数据吗?

iphone - 如何获得 objective-c 中两个位置之间的距离(以英里为单位)?

ios - 如何让 UIBarButton 执行自定义功能?