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

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

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

How can I make my navigation bar uniformly semi-transparent?

I'm writing an app on iOS 7, and I can't seem to get a handle on the transparency of the navigationBar and the toolbar, how can I set the navigation bar to black at 50% opacity?
I've read the transition to ios7 guide and I've watched the wwdc13 lecture 214, but my status bar still has a different transparency than the rest of the attached nav bar.
Here is my code:
// APP-WIDE THEMING
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackOpaque];
[[UINavigationBar appearance] setBackgroundColor:[UIColor blueColor]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
Here is the screenshot of my problem: http://grab.by/qiyU
Set the background image to nil, and set the background color with alpha.
[ctrl.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
ctrl.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:0 Green:0 Blue:0 Alpha:.5];
According to the answer posted here it's possible to create a transparent UINavigationBar:
How to draw a transparent UIToolbar or UINavigationBar in iOS7
However you want to create a semitransparent navigationbar. For that as far as I can tell you have to create a 1 px large image containing a black color with 50% opacity. Add this as backgroundimage for your Navigationbar.
This snippet should do the trick:
[[UINavigationBar appearance] setBarStyle:UIBarStyleDefault];
UIImage* sti = [UIImage imageNamed:#"EMT_SemiTransparent.png"];
[[UINavigationBar appearance] setBackgroundImage:sti forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:UIColor.clearColor];
try setting key [View controller-based status bar appearance] to NO in your pList file as well.
i've run into some funkiness with the status bar not seeming to be affected by changes in code and this solved it for me.
reference: https://stackoverflow.com/a/18184831/2962193
Set the alpha value to make it transparent.
[[UINavigationBar appearance] setAlpha:0.5f];

iOS: Background image in UIImagePickerController is inconsistent with the rest of the app

I'm using the following code to set the navbar's background:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"tile.png"] forBarMetrics:UIBarMetricsDefault];
This results in a nice tiled background such as you see here:
However, when I open up a UIImagePickerController, the background is warped somehow and we end up with something like this:
Anyone have suggestions on how to fix it?
Stretching instead of tiling: to Stop That
Create a class of type UINavigationBar
Create your custom UINavigationBar subclass.
Comment initwithFrameMethod
Add method DrawRect:
(void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"bluebackground.jpeg"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
4.in AppDelegate.m, AppliocationdidFinishLaunching
Write
[self.navigationbar setValue:[[customBar alloc]init] forKeyPath:#"navigationBar"];
Adding Image By this way will not stretch the image
Refer this Link:
http://www.iosdevnotes.com/2011/09/custom-uinavigationbars-techniques/
http://designm.ag/tutorials/designing-a-custom-iphone-app-navigation-bar/
Hope this will help you.

UITabbar with partially transparent background

Long time reader, first time poster. Please be gentle :P
I'm working on an app and have a background image for a UITabbar element that is transparent for a few pixels at the top. I have searched far and wide, tried many solutions suggested that I could find (setting tint, background color to clearColor, setting alpha to 0.2, etc) but I continue to see a black line where the tabbar should be transparent. I am setting the background as follows (see the lines commented out for some more things I have tried which did not work)
//[[UITabBar appearance] setBackgroundColor:[UIColor clearColor]];
//[self.view setBackgroundColor:[UIColor clearColor]];
//[super.view setBackgroundColor:[UIColor clearColor]];
//UITabBar.super.setBackgroundColor:[UIColor clearColor]];
[[UITabBar appearance] setBackgroundColor:[[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:0.2]];
//[[UITabBar appearance] setTintColor:[UIColor blackColor]];
[[UITabBar appearance] setTintColor:[[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:0.1]];
//[[UITabBar appearance] setAlpha:0.0];// setTintColor:[UIColor magentaColor]];
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:#"tabbar.png"]];
Screenshot can be fount at http://i.imgur.com/m1tW5.png
The app uses a tabbarcontroller.
When I set the background color to white or any other color, the black line changes color successfully but as soon as I set the color to clearColor the black line comes back.
When I hide the entire tab bar there is nothing crazy behind it and I can successfully see the cream background as I should.
The image is a png with a transparent top where the black line appears as mentioned earlier.
Does anyone have any suggestions on what I could be doing wrong?
Help will be truly appreciated.
Edit: I am using iOS 5.0 and don't care about supporting previous iOS versions (in case that opens up any additional potential choices).
I'm currently trying to solve a similar problem.
I believe the black color is either:
The TabBars Superviews background in which case you can try to set the tabBars superviews background color. I don't have my mac here so I can't try it and I don't see that in your "tried" code.
Or it's the border of the tabBar, in which you can change the bordercolor of the tabBar's frame.
tabbar.frame.borderColor = [UIColor whiteColor];
You will need to import the QuartzCore framework to manipulate the boderColor.
Let me know if it helps.
I decided to go with a "hack-ish" solution I came across elsewhere.
I'm using the same color as the background in areas of the tab bar's background image to create the illusion of transparency. Not really transparent but that's the best I could come up with after countless hours spent on this.
Maybe that'll be a solution that works for someone else in a similar position in the future as well.

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.