UINavigationBar setBackgroundImage sometimes doesn't take immediate effect - objective-c

I'm resetting the NavigationBar's background image by calling setBackgroundImage when showing an MFMailComposeViewController. But when I try to reset the image to its original by calling setBackgroundImage a second time in mailComposeController:didFinishWithResult:, it doesn't visibly change the background image.
The weird thing is that the log shows me that the NavigationBar's background image has changed, and after I pop a few view controllers (I'm not sure which are modal transitions and which aren't, because it's somebody else's code), the background image finally does change. But why doesn't it change immediately?
Here's my relevant code inside mailComposeController:didFinishWithResult::
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"img"]
forBarMetrics:UIBarMetricsDefault];
NSLog(#"Navbar img = %#", [[UINavigationBar appearance]
backgroundImageForBarMetrics:UIBarMetricsDefault]);

Another suggestion is to make sure that you are setting the background image after you've dismissed the mailComposeController and after your own view controller has become active again.

Are you sure that your -mailComposeController:didFinishWithResult: is being called on the main thread, where you need to be doing all of your ui work?
Try executing your -setBackgroundImage:forBarMetrics: call on the main thread and see if this helps.
For example:
dispatch_async(dispatch_get_main_queue(), ^{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"img"]
forBarMetrics:UIBarMetricsDefault];
});

Related

Cannot change UITabBar background image second time

When I try to change the background image for a second time for a tab view, nothing happens.
I'm using:
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:newBarImageBackGround];
It seems that you need to use something like the following for changing the tab bar's background. Unfortunately, this doesn't fly with appearance.
[[self.tabController tabBar] setBackgroundImage:[UIImage imageNamed:newBarImageBackGround]];
Your code should work for the first time though. Reference: Link
Note: [self.tabController tabBar] should be replaced with whatever reference to a tab bar you're using.
EDIT
You could also try this, but I haven't tested it so I don't know if it'll work.
[[UITabBar appearance] setBackgroundImage:nil];
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:newBarImageBackGround]];

Change MFMailComposeViewController's UINavigationBar color in iOS7

I am trying to update my app for iOS7, however I am unable to set the colour of the UINavigationBar at the top of the screen. I have tried the classic approach...
[mailer.navigationBar setTintColor:[UIColor darkGreyColour]];
...however it didn't work. I have also tried calling setBarTintColor instead, however that didn't seem to work either.
Does anyone know if its possible to set the colour and if so how?
I had your same problem when updating my application for iOS 7.
I solved using this line of code (if you use images):
[[UINavigationBar appearance] setBackgroundImage:#"myImage" forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
or this one if you are not using images:
[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];
I placed then in AppDelegate, just before returning application:didFinishLaunchingWithOptions:
Hope this helps!
Try changing the tint in storyboard. Its in 'view' sub section when you select navigation bar in IB.

UIBarButtonItem image (or UIToolbar custom background image?..) is darker (or lighter?..) than original

I want to create custom buttons for UIToolbar (and a custom background color for UIToolbar) in XCode.
So far, I've done it like this: create UIButton, then set an image to it and then drag it to UIToolbar. It becomes UIBarButtonItem after this, but still it has an image so it's fine for me.
The problem is, that for some reason buttons' images became darker than original images (I can see it because my toolbar background is exactly the same as buttons' background), but when I press the button, I see that its image becomes original-like and then after a sec turns back to the darker one.
I've tried to do as said in this question: Image in UIButton is darker tharn original, setting highlighted image in XCode, but after this it stopped 'changing to original' at all.
So, how can I solve this? I don't really want to bring some GUI stuff like this in code if there's a way to do this by XCode... Any help would be appreciated.
EDIT:
That's the screen shot of what's going on:
And that's how they should look like with toolbar background + buttom image combined:
Now I did it in code like this:
#implementation UIBarButtonItem (CustomView)
+ (UIBarButtonItem*) barItemWithImage:(UIImage *)image target:(id)target action:(SEL)action
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* item = [[self alloc] initWithCustomView:button];
return item;
}
#end
And the calling code:
UIBarButtonItem *item =[UIBarButtonItem barItemWithImage:[UIImage imageNamed:#"settings"] target:nil action:nil];
[self.bottomBar setItems:#[item]];
For toolbar background I use this code:
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:#"toolbar_background"]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
The problem remains the same...
EDIT 2:
Wow, it seems like actually the UIToolbar is lighter than original...
EDIT 3:
Well. Created new project and tested the XCode storyboard-way (with UIButton), there is works fine... That's a mystery for me, totally lost here. Need some serious clarification here about possible reasons of this: my current project is huge and I'm not able to post all the code here...
Well, since nobody answered, I'll just say that I created a new project and the problem gone.
But it happened once again, and I totally don't know why. I did a really lazy and bad thing and changed image brightness for that unlucky button in some image editor =D

UINavigationBar setBackgroundImage:forBarMetrics: portrait/landscape weirdness

Switching to iOS5, I wanted to make use of a custom styled UINavigationBar using the following code snippet:
if ([navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:
forBarMetrics:)]) {
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"NavigationBar.png"]
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"NavigationBarLandscape.png"]
forBarMetrics:UIBarMetricsLandscapePhone];
}
This works quite well, but: my custom image has rounded corners (c.f. the attached images) which causes the following:
Starting in portrait mode, it looks fine.
Changing to landscape mode also looks good.
Changing back to portrait mode however does not seem to change the image to NavigationBar.png, thus there is no rounded corner on the top right.
Any advice on how to solve this problem is appreciated.
// edit
Adding the following code to my viewController removes this "bug" but this cannot be the right solution!?!
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation
duration:duration];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"NavigationBar.png"]
forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"NavigationBarLandscape.png"]
forBarMetrics:UIBarMetricsLandscapePhone];
}
As iOS6 introduces rounded corners by default, this problem will disappear soon.

Why is setting my UINavigationBar's background image affecting the size of the navigation bar?

I'm doing something like this:
UIImage *barBackgroundImage = [UIImage imageNamed:#"myImage.png"];
[[UINavigationBar appearance] setBackgroundImage:
backgroundImage
forBarMetrics:UIBarMetricsDefault];
And the resulting UINavigationBar is much larger than the standard navigation bar. I'm aware that my image size might be the problem. Is there any way to take care of this programmatically?
This is how I set the UINavigationController's navigationBar background image.
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbar.png"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setTintColor:LIGHTBROWN];
navbar.png is 320x44 and respectively 640x88 for navbar#2x.png.
Follow this link to make your code compatible with iOS4, 5 and 6: http://robert-varga.com/?p=735
You just have to make in Photoshop or other software a rectangular with the size of 320x44 or 640x88 (for retina display) and import it to your project