无需重新启动应用程序即可更改 UINavigationBar 背景图像 [英] Changing the UINavigationBar background image without restarting app
问题描述
现在,我有
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed: @"UINavigationBarBackground.png"]
forBarMetrics:UIBarMetricsDefault];
我希望它在单击时更改背景.现在,它仅在您重新启动应用程序后才会更改.有没有办法让它改变导航栏的背景图像而不必重新启动应用程序?
I want it to change the background on-click. Right now, it only changes after you restart the app. Is there a way to have it change the background image of the navigation bar without having to restart the app?
我试过了
[[UINavigationBar appearance] setNeedsDisplay];
但它崩溃了:由于未捕获的异常'NSInvalidArgumentException',终止应用程序,原因:'-[NSMethodSignature getArgumentTypeAtIndex:]: index (2) out of bounds [0, 1]'
but it crashes with: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSMethodSignature getArgumentTypeAtIndex:]: index (2) out of bounds [0, 1]'
如果我尝试
[self.navigationController.navigationBar setNeedsDisplay];
或
[self.view setNeedsDisplay];
什么都没发生.
推荐答案
UINavigationBar 只是 Apple 在 iOS 中提供的另一个(但有点特殊)子类 UIView,因此您应该能够使用诸如 "setNeedsDisplay
" 调整图像后立即.
UINavigationBar is just another (but slightly special) subclassed UIView that Apple provides in iOS, so you should be able to force an update using something like "setNeedsDisplay
" right after you adjust the image.
这篇关于无需重新启动应用程序即可更改 UINavigationBar 背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!