Unable to make Navigation Bar totally transparent in iOS6 - ios

I was using the following code to make my Navigation Bar transparent in iOS5:
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];
Upgraded to iOS6 and the Navigation Bar is still transparent but now has a thin black line underneath it. How can I make the Navigation Bar totally transparent?
I have also tried all of the following:
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.opaque = YES;
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
[[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
Thanks in advance.

Solved. iOS6 has added a drop shadow to the Navigation Bar. So the masking code that I was using with iOS5 still works fine - I just need to add
if ([self.navigationController.navigationBar respondsToSelector:#selector(shadowImage)])
{
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}
to get rid of the drop shadow.

self.navigationController.navigationBar.translucent = YES; // Setting this slides the view up, underneath the nav bar (otherwise it'll appear black)
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];
//remove shadow
[[UINavigationBar appearance] setShadowImage: [[UIImage alloc] init]];

if ([self.navigationController.navigationBar respondsToSelector:#selector(shadowImage)])
{
[self.navigationController.navigationBar setShadowImage:[[[UIImage alloc] init] autorelease]];
// autorelease is necessary, or else [[UIImage alloc] init]'s retainCount is 2.
}

Related

Nav bar is translucent, but I would like transparent

I have followed the top guides on how to make my top bar transparent, but the best I can get is translucent. I am also trying to get the buttons white, not default blue. What am I missing?
#implementation FindAssetLocationMapViewController
- (void) viewWillAppear:(BOOL)animated {
self.edgesForExtendedLayout = UIRectEdgeAll;
self.extendedLayoutIncludesOpaqueBars = true;
[self.navigationController.navigationBar setTranslucent:YES];
self.navigationController.navigationBar.shadowImage = [UIImage new];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.view.backgroundColor = [UIColor redColor];
}
Images are below. Should I take screenshots of the FindAssetLocationMapViewController attributes as well? Just to clarify, the nav controller and nav bar attributes do not have a class associated with them.
Try this code in your app delegate, then remove the code you have:
+ (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0, 0, 1, 1);
// create a 1 by 1 pixel context
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[color setFill];
UIRectFill(rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Place this inside application:didFinishLaunchingWithOptions:
//create background images for the navigation bar
UIImage *clear = [AppDelegate imageWithColor:[UIColor clearColor]];
//customize the appearance of UINavigationBar
[[UINavigationBar appearance] setBackgroundImage:clear forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:clear forBarMetrics:UIBarMetricsCompact];
[[UINavigationBar appearance] setTranslucent:NO];
[[UINavigationBar appearance] setShadowImage:[UIImage new]];

How to setup a navigation bar with image?

Something weird and frustrating is happening with the UINavigationBar on iOS7. I'm trying to add an image like this:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"Logo_Small.png"] forBarMetrics:UIBarMetricsDefault];
The result is that the lettering of my header -- the Hebrew letters -- repeat all over the navigation bar.
I tried all four options for the UIBarMetrics flag, but with the other three the logo just disappears. I also set the image size to 320x64, as per the documentation. But nothing seems to do the right thing, and I'm on the verge of giving up using a logo and just have a controller.title.
Do the below:
UIImage *gradientImage = [[UIImage imageNamed:#"Logo_Small.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(-4, 300, 10, 300)];// play here
[[UINavigationBar appearance] setBackgroundImage:gradientImage forBarMetrics:UIBarMetricsDefault];
you can do like this
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,44)];
iv.image = [UIImage imageNamed:#"Logo_Small.png"];
self.navigationItem.titleView = iv;
Use this,
UIImage *navBarImage64 = [[UIImage imageNamed:#"Logo_Small.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navBarImage64 forBarMetrics:UIBarMetricsDefault];
If above is not working then add imageview in your NavigationBar as below code. it work.
UIImage *image = [UIImage imageNamed: #"NavigationBar#2x.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame = CGRectMake(0, 0, 320, 44);
[self.navigationController.navigationBar addSubview:imageView];
You code is actually works. The problem is that you are probably calling it after UINavigationBar was added to screen.
To verify that your code is working you can try to put into AppDelegate's didiFinishedLaunchingWithOptions.
To customize navigation bar with Image use this,
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"ImageName.png"] forBarMetrics:UIBarMetricsDefault];

How to apply a background image to navigation bar

I want to insert an image to the navigation bar.
My code is
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"bodyBg.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
I have attached the screenshot of the current output. Is this because of the large image size?
But my desired output is
First, make your navigation bar image size 1024x44 pixels nad for retina display 2048x88 pixels.
If you have the same image for UINavigationBar on every view controller, put this to AppDelegate in method didFinishLaunchingWithOptions:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"nav-background.png"] forBarMetrics:UIBarMetricsDefault];
// This will remove shadow in iOS6
if ([[UINavigationBar class] instancesRespondToSelector:#selector(shadowImage)]) {
[[UINavigationBar appearance] setShadowImage:[[[UIImage alloc] init] autorelease]];
}
And also I see you need custom back button, also put this in AppDelegate:
UIImage *backButtonNormal = [UIImage imageNamed:#"nav-back.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
you can set the color of navigation bar same as your image use below code and you can put image name which you want to show in navigation bar
self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bodyBg.png"]];
try it out with this code..
if ([navBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)])
{
[navBar setBackgroundImage:[UIImage imageNamed:#"Nav.png"] forBarMetrics:UIBarMetricsDefault];
}
else
{
UIImageView *imageView = (UIImageView *)[navBar viewWithTag:1];//any tag
if (imageView == nil)
{
imageView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"Nav.png"]];
[navBar insertSubview:imageView atIndex:0];
[imageView release];
}
}
see my full answer from this link setting-title-for-uinavigation-bar-in-iphone
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navBar"] forBarMetrics:UIBarMetricsDefault];
I hope , it will work for u
Use UINavigationBar's setBackgroundImage: forBarMetrics: method :
[self.navigationController.navigationBar setBackgroundImage:yourImageHere forBarMetrics:UIBarMetricsDefault];
your UIImage should be of appropriate height and width. For example iphone 4 retina size would be 640*88 for portrait.
EDIT : Point here is if UIImage height is bigger say 150 pixel height it will display that much height and our UINavigationBar height is 44 pixel.
check this link for add background image in navigationbar and first you your navigationbar
image size set as navigationbar.
check this link here
i hope this code useful for you.
[myNavbar setBackgroundImage:[UIImage imageNamed: #"UINavigationBarBackground.png"]
forBarMetrics:UIBarMetricsDefault];
and also this link
Try this code it works for me:
UIView* navigationBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIImage * targetImage = [UIImage imageNamed:#"Header_top.png"];
UIGraphicsBeginImageContextWithOptions(navigationBGView.frame.size, NO, 0.f);
[targetImage drawInRect:CGRectMake(0.f, 0.f, navigationBGView.frame.size.width, navigationBGView.frame.size.height)];
UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
navigationBGView.backgroundColor = [UIColor colorWithPatternImage:resultImage];
[self.navigationController.navigationBar addSubview:navigationBGView];
[self.navigationController.navigationBar setTintColor:[UIColor clearColor]];

How do I remove the gradient from UIToolbar in iPad?

I am using UIToolbar in iPad App,How can I remove the gradient background from it? Below is my Code.
aToolbar.barStyle = UIBarStyleBlackTranslucent;
aToolbar.tintColor = [UIColor blackColor]; toolbar.alpha = 1.0;
You need to set image in UIToolbar so remove Gradient.
My suggestion is please set image and create custom UItoolbar.
For that code is below.
UIImage *gradientImage44 = [[UIImage imageNamed:#"imageName.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Set the background image for *all* UINavigationBars
[[UIToolbar appearance] setBackgroundImage:gradientImage44
forBarMetrics:UIBarMetricsDefault];

set background image navigation bar

So I am try to set a background image for a navigation bar in MonoTouch. The image will be the same everywhere. How would i do this?
In the viewdid load: NavBar.SetBackgroundImage(UIImage.FromFile ("btn-title-bar.png"));
doesn't work.
Try
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"btn-title-bar.png"] forBarMetrics:UIBarMetricsDefault];
In c# land that is:
UINavigationBar.Appearance.SetBackgroundImage (UIImage.FromFile ("btn-title-bar.png"), UIBarMetrics.Default);
Try this below code.
UIImage *image = [UIImage imageNamed: #"NavigationBar#2x.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame = CGRectMake(0, 0, 320, 44);
[self.navigationController.navigationBar addSubview:imageView];
if you want to put different images on every page then write this method in view did load on every page..
UIImage *image = [UIImage imageNamed: #"logo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
and if u want single image on every page then write this code in delegate.m.
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"logo.png"]
forBarMetrics:UIBarMetricsDefault];

Resources