I'm using UIAppearance in iOS 5 to create a custom UINavigationBar. I use the following code (inside AppDelegate.m) to give all of the navigation bars the same background
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"NavBarNoText.png"] forBarMetrics:UIBarMetricsDefault];
I'd like to be able to use 1 navbar "NavBarWithText.png" for only 1 TableViewController, while I use the NavBarNoText.png as the background for all of the other navigation bars except for said TableViewController. Does anyone know if this is possible? I know Instagram does something like it, with the home tab using a different navigation bar than all of the other tabs.
I've tried using this separately in the ViewDidLoad method in each separate class, but it doesn't seem to work. I get the single view to have a different background at first, but it changes back to the common background after I switch a view.
-(void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"NavBarWithText.png"] forBarMetrics:UIBarMetricsDefault];
}
For the single view
-(void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"NavBarNoText.png"] forBarMetrics:UIBarMetricsDefault];
}
For all of the other views.
You can use this appearanceWhenContainedIn: to show different navigationBar when displayed in UITableViewController for more information see here
Related
I have a UINavigationController with 3 viewControllers. We know the three viewControllers share a common navigationBar.If I want to set the navigationBar totally transparent. I can put the code in viewWillAppear:
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setShadowImage:[UIImage new]];
[self.navigationBar setBarTintColor:[UIColor clearColor]];
self.navigationBar.translucent = YES;
and set it back in viewWillDisappear:
[self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self setShadowImage:nil];
[self setBarTintColor:THEME_COLOR];
self.translucent = NO;
I want to set the UINavigationBar translucent only in viewControllerB, so I put the code in viewControllerB. However, when I popToViewController B, I can see a black bar in the top right of the screen. Since the viewWillAppear is invocated. It seems can not be solved in my case.
I come out with some methods:
use different UINavigationBar.
use different UINavigationController. But UINavigationController can not push a new UINavigationController
Custom UIView like UINavigationBar.
I think above methos is more complicated。
Any ideas thanks!
That black color you see is the background color of main window. You can set background image or color to your main window from AppDelegate didFinishLaunchingWithOptions method (That's totally depends with your design of the view controller B) so that you won't see any difference.
Or else
Simply you can use viewDidAppear instead of using viewWillAppear, but that will have little flick though.
I have a container view with two views and a toolbar. In the ContainerViewController I have "deactivated" the shadow of the navigation bar using this code (from Apple's library):
[self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:#"TransparentPixel"]];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"Pixel"] forBarMetrics:UIBarMetricsDefault];
Also I have implemented the UIToolbarDelegate and its method
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
return UIBarPositionTop; //or return UIBarPositionTopAttached;
}
But the shadow of the toolbar is still between the navigation bar and the toolbar and not underneath the toolbar, to separate the toolbar from the content.
Any ideas why the shadow doesn't change? Or how to check, if the toolbar position is position really TopAttached.
Edit:
I try to do something like described in this post: UISegmentedControl below UINavigationbar in iOS 7 without the searchbar.
As explained there, there are two shadows, the one of the navigationbar in the navigationcontroller, that is between navbar and toolbar. And the shadow of the toolbar.
I was able to deactivated the shadow of the navigationbar as described. It works also in the viewDidLoad:
But the shadow of the toolbar is in the wrong position. Using the positionForBar: method it should be in the button of the toolbar, but it stays in the top (between Navbar and Toolbar).
So I am looking for the fault, why it does not change.
You didn't specify what method you tried to change the shadow in, but I'm going to guess that you're trying to do it in viewDidLoad:
Your navigationBar does not actually exist in viewDidLoad, instead change the shadow in viewWillAppear: like this:
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:#"TransparentPixel"]];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"Pixel"] forBarMetrics:UIBarMetricsDefault];
}
I am trying to change the color of the UINavigationBar as the User navigates to a different page. However my App does not change the color of the UINavigationBar until the user backs out of the page and goes back into the page once again. I tried putting the initialization code for changing the color in -(void)viewDidLoad and also -(void)viewWillAppear:(BOOL)animated. Here is how I am currently attempting to change the color:
-(void)viewWillAppear:(BOOL)animated{
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0.32f green:0.14f blue:0.32f alpha:1.00f]];
}
You should use something like that:
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.32f green:0.14f blue:0.32f alpha:1.00f]];
}
That code you have changes title bar color for ALL UINavigationBar instances. It's application wide proxy. You don't want to use it if you need to change it in just one place.
I added the following code in the appDelegate to achieve custom background images for the navigation bar.
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"Nav-Bar_01.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"Nav-Bar_02_alter.png"] forBarMetrics:UIBarMetricsDefaultPrompt];
However, I want to exclude the native gallery pickerView from this background.
Is there any way to do so without applying the background on each navigation bar alone, but keeping the same code in the appDelegate ?
I ended up placing a background image for every navigation controller instead of placing it in the App Delegate. The background was then not applied on the pickerView.
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"Nav-Bar_01"] forBarMetrics:UIBarMetricsDefault];
I'm working on a simple application with 3 tabs at the moment, the first tab is a UITableViewController, the other 2 are UIViewControllers.
Now from inside both UIViewControllers, I can set self.title and the title of the navbar will change.
But if I try this from inside the UITableViewController, this doesn't work, why is this?
I'm also trying to set a custom background for the navigation bar, after checking some tutorials, I found that this should work, but it really doesn't
UIImage *image = [UIImage imageNamed:#"bgNavigationBar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
self.navigationItem.title=#"Your Title";
should work, just put it in
- (void)viewDidLoad {
}
of the UITableViewController