I have strange bug(?)
If I call setBackgroundImage:forToolbarPosition:barMetrics: from UIApplicationDelegate application:didFinishLaunchingWithOptions: , it works fine and toolbar background changes to my image.
If I try to set this call to another place, for example viewDidLoad, it does not work.
The code is quite simple,
This code works:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"bg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
return YES;
}
and this does not:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"bg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
}
Once you call the method the system takes the new appearance proxy for all the UIToolBars in the app, but you need to call [navitagtionController.view setNeedsDisplay]; so the layer of the controller its drawn, specially if you call it in the viewDidLoad of your root controller in the navigation controller.
Remember that iOS native controllers are pretty demn efficient in layer drawing, so it will only update the frame of the root view controller when the controller view its been drawn (or as minimum as it needs to update actually); if i don’t mistake even setting the title of the navigation controller will make the trick (but pressing buttons or changing in them wont have any effect for the same reasons than only the buttons layer are been updated).
In this case call [myToolbarInstance setNeedDisplay] after creating the proxy. This way you can change the appearance of any controller at any time, or even save some redrawing calls taking advantage of it.