Cannot change UITabBar background image second time - objective-c

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]];

Related

UINavigationBar color has changed after upgrading to latest Xcode

Recently I've updated my Xcode IDE to the latest to have iOS 7.1 SDK. For some reason, after recompiling my app with the new Xcode I get this wired UINavigationBar background colour.
I'm using storyboard to push the secondary view controller from home screen. The secondary view controller is a static table of settings, also built using storyboard, and contains the following code in its viewDidLoad method:
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = nil;
I haven't done any other changes to this screen or any other screens in the app.
This is how it used to look like before:
This is how it is look like now:
Does anyone have any idea what could have caused this?
Cheers,
EDIT: #Leo Natan -
When I'm trying to set the color myself, for example, by replacing this line:
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
with this line:
[self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];
I get this: the red color applied only on the header without the status bar, what can I do?
Latest Xcode comes with iOS7.1 SDK, in which Apple once again changed how the color is calculated. You will have to play again with your color to achieve a similar look in iOS7.1.
Maybe you should take another approach. Use the Appearance Proxy in the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions. Thats how I do it:
//Navigationbar
[[UINavigationBar appearance] setBarTintColor:lightOrangeColor];
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"navbarAssets-backArrow-iOS7"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"navbarAssets-backArrow-iOS7"]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes: #{NSForegroundColorAttributeName : [UIColor whiteColor]}];

UINavigationBar setBackgroundImage sometimes doesn't take immediate effect

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];
});

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.

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