How to set the background of UINavigationBar in Objective-C ? - ios

I am trying to change the background of UINavigationBar.
I already put the image to Images.xcassets , and the xib file is TableView
I have write [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"bar-background.png"] forBarMetrics:UIBarMetricsDefault]; in the viewDidLoad like the following.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIView* bview = [[UIView alloc] init];
bview.backgroundColor = [UIColor groupTableViewBackgroundColor];
//self.title = NSLocalizedString(#"WiFi Camera Viewer", nil) ;
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"bar-background.png"] forBarMetrics:UIBarMetricsDefault];
}
But the UINavigationBar did not show any image like the following
How to set the background of UINavigationBar in Objective-C ?
Did I missing something ? Thanks in advance.

The code you are using is old, starting from iOS 7 you could use the below
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"YourImage"] forBarMetrics:UIBarMetricsDefault];
If you provide an image with 44px height, it'll cover the navigation bar only without the status bar, if you provide it with 66px height, it'll cover them both

Related

Transition to UINavigationBar with transparent background image results in black flickering

I want to navigate from a white UINavigatinonBarto a transparent UINavigationBar.
In my root view controller, this is the setup:
self.navigationController.navigationBar.hidden = NO;
[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setTranslucent:NO];
It's important that it stays non translucent.
This is the setup in the second view controller:
// set title of navbar
self.title = [self.data objectForKey:#"title"];
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
//
// IMPORTANT PART: make the nav bar transaparent - no prerequisites.
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
// set custom back button
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(16, 31, 22, 20)];
UIImage *backImage = [UIImage imageNamed:#"backButtonDetail"];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
This goes almost well, except for the animation:
As you can see, the background becomes black when I animate it. I want to fade the white into the destination color. It now animates from the black background.
I tried settings a custom UIView underneath the nav bar, but that didn't work.
I'm quite lost about how to about this now and any help would be highly appreciated.
I recently ran into this same problem and was able to get around it by just setting the window's background color in the appDelegate's didFinishLaunchingWithOptions
window.backgroundColor = UIColor.whiteColor()
and if you don't already have a reference to window then
UIApplication.sharedApplication().delegate?.window.backgroundColor = ...
I found this to be the cleanest solution. Hope it helps, albeit a bit late.

How to make UINavigationBar fully transparent or gone like this only title and UIBarButtonItem could be seen

As you can see, the UINavigationBar is fully transparent. Only the title and UIBarButtonItem is visible. And the status bar has the same colour as UITableView's background colour. Right now, I have finished to make the table view and cell has the same effect as the pic. But how to make the navigation bar and the status bar has the effect too?
Try this one
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
Try this !
[self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
[self.navigationController.view setBackgroundColor:[UIColor clearColor]];
[self.navigationController.navigationBar setBarTintColor:[UIColor clearColor]];

Different UINavigationBar background for different views

I am creating my UINavigationBar in code like so:
// init and add root navigation controller to view
_rootNavigationController = [[UINavigationController alloc] init];
[_rootView.contentContainer addSubview:_rootNavigationController.view];
_rootNavigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
Then inside a certain UIViewController I want the UINavigationBar to have a clear background, so i use this code which works:
UINavigationBar *navBar = self.navigationController.navigationBar;
navBar.topItem.title = #"";
[navBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[navBar setShadowImage:[UIImage new]];
[navBar setTranslucent:YES];
[navBar setTintColor:[[AVOThemeManager sharedTheme] contentBackButtonColor]];
Once the user leaves this page by pressing back, I want to return my UINavigationBar back to how it origonally looked, so I am trying this:
- (void)viewDidDisappear:(BOOL)animated {
UINavigationBar *navBar = self.navigationController.navigationBar;
[navBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[navBar setShadowImage:nil];
[navBar setBarStyle:UIBarStyleBlackTranslucent];
[navBar setTranslucent:YES];
}
But this does not seem to work. I also do not want to set the UINavigationBar to be a new instance, and then set the properties, because it has a global button which is added in the root navigation controller, which the child view controllers don't know about.
Is there anything else I can try?
After posting, I immediately found the answer. Basically instead of using viewDidDisappear, I should use viewWillDisappear and it worked.

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

Custom transparent UITabBar

I am trying to create an app in which the tab bar transparent (or which looks transparent). For this i'm using part of an image in the view. And set the remaining part as background for tab bar. The problem is the tab bar appears with a darker shade. I'm out of ideas now.
Below is the screen shot
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tabbar_bg.png"]];
self.tabBar.backgroundColor=[UIColor whiteColor];
[self.tabBar insertSubview:imgView atIndex:1];
// [self.tabBar setSelectionIndicatorImage:[UIImage imageNamed:#"transp.png"]];
// [self.tabBar setSelectedImageTintColor:[UIColor clearColor]];
[[UITabBar appearance] setTintColor:[UIColor clearColor]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor clearColor]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"transp.png"]];
The above is the code that i've tried out in the viewDidLoad of my custom UITabBarController class. "tabbar_bg.png" is the image used as background for the tab bar and 'transp.png' is a transparent image used as the selectionIndicator image.
Thanks for the help.
Try
[self.tabBar setBackgroundImage:[UIImage new]];
This trick also works for UINavigationBar.
Have you tried creating a subclass of it?
#implementation TabBarInvisible
-(id)init
{
self = [super init];
if (self)
{
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
self.backgroundImage = nil;
}
return self;
}
-(void)drawRect:(CGRect)rect
{
}
#end

Resources