Changing the UINavigationBar background image without restarting app

自闭症网瘾萝莉.ら 提交于 2019-12-12 01:32:06

问题


Right now, I have

[[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?

I tried

[[UINavigationBar appearance] setNeedsDisplay];

but it crashes with: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSMethodSignature getArgumentTypeAtIndex:]: index (2) out of bounds [0, 1]'

And if I try

[self.navigationController.navigationBar setNeedsDisplay];

or

[self.view setNeedsDisplay];

nothing happens.


回答1:


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.



来源:https://stackoverflow.com/questions/21121218/changing-the-uinavigationbar-background-image-without-restarting-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!