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.
Related
I've read a lot of posts on here and tried most of the options mentioned, but none fix the issue for me. I have an app that is based off of a Tab Bar Controller. Each tab is a UIViewController with a Navigation Bar at the top.
Adding this code to the AppDelegate gives me an orange coloured Navigation Bar with white text, but a white status bar with Black text.
[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
Reading the answers on various pages suggest adding the following to the View controller:
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
Then calling this in View Did Load:
[self setNeedsStatusBarAppearanceUpdate];
This gets me a White status bar with White text, how can I now get the status bar to go orange to match my Navigation Bar??
The solution mentioned on here https://stackoverflow.com/a/19513714/505457 for those using a Navigation Controller doesn't work, I guess thats because my main controller is a Tab Bar Controller.
Anyone come across this before? Thanks in advance for any advice / suggestions you may have. I can provide a sample app if required, but its probably as quick to build one with the Tab Bar template, add a Navigation bar then paste in my code samples.
Plasma
You can find the statusBar UIVIew by it's name and tint it. Add this method to your AppDelegate.m and call it from didFinishLaunchingWithOptions:
- (void)setStatusBarBackgroundColor:(UIColor *)color {
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:#"statusBarWindow"] valueForKey:#"statusBar"];
if ([statusBar respondsToSelector:#selector(setBackgroundColor:)]) {
statusBar.backgroundColor = color;
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
[self setStatusBarBackgroundColor:[UIColor orangeColor]];
...
}
Note: There are apps in store that use this method. So it is okay with the apple HIG policy.
Well I had almost the same problem since IOS7 , But my quick solution is to change all your views from the first one, I am refering to set up your color bar configuration in the AppDelegate.m file, I recommend to use this free framework Nab Bar Color And Gradient is very easy to use and also you will be available to set a beautiful gradient on all of your views.
See the examples in the project.
Normally, you shouldn't be adding a UINavigationBar to a UIViewController. Instead, fill your UITabBarController will UINavigationControllers.
let firstViewController = FirstViewController(nibName: nil, bundle: nil)
let firstNavigationController = UINavigationController(rootViewController: firstViewController)
let secondViewController = SecondViewController(nibName: nil, bundle: nil)
let secondNavigationController = UINavigationController(rootViewController: secondViewController)
let navigationControllers = [
firstNavigationController,
secondNavigationController
]
yourTabBarController.setViewControllers(navigationControllers, animated: false)
The same general process applies if you are using storyboards.
Right so the point BSmith11 made about adding Navigation controllers to a Tab Bar got me thinking. So I did a bit of Googling and an answer on this page, helps a lot: Tab bar controller inside a uinavigationcontroller
By having the TabBar controller as the rootViewController, then inserting a "NavigationController" between that and the normal ViewController allows me to do this:
//Fix up the Navigation bar tint and set text colours to white
[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor orangeColor]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
//Also requires setting 'View controller-based status bar appearance' to NO in .plist
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
Which is exactly what I had before, and that gives me a coloured StatusBar and the exact same colour NavBar. Now to see if I can add it in to the existing app and not break everything.
Plasma
Some normal code looks like this:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setBarTintColor:someColor];
}
or
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController.navigationBar setBarTintColor: someColor];
}
However, this causes the navigation bar suddenly and completely change it's color upon push event occurs. (or upon you drag the left edge of a controller to pop it, which triggers 'viewWillDisappear' method) Is there any way that I can keep both color presenting during the pop animation?
I know weChat can do that...
As the photo below, while the controller at right side is being popped, the light gray background color (maybe background image) of it's navi bar is there, and the black color of the navi bar of the controller at left side is also there.
I solved it by making the the navigation bar transparent in both views, and I added the colors I want in the views itself with a rectangular view just for this to use as background color.
here the storyboard:
and here the final result:
Some code (make sure you don't hard code the width of the view like in this example):
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 480, 60)];
bg.backgroundColor = [UIColor redColor];
[self.view addSubview:bg];
And make sure you disable "Adjust ScrollView Insets"
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];
}
Note, I've recently come back to this and it seems to have been fixed in more recent versions of the SDK, without me having to implement anything except the code in the question. Many thanks to all who answered.
I have an Objective-C app that runs on the iPad and displays a view controller with a modal presentation style of UIModalPresentationPageSheet:
UINavigationController *editorNavigationController = [[UINavigationController alloc] initWithRootViewController:editorViewController];
editorNavigationController.modalPresentationStyle = UIModalPresentationPageSheet;
[navigationController presentViewController:editorNavigationController animated:YES completion:nil];
When this view controller is displayed the buttons in the navigation bar are purple, which I assume has been picked up from the window's tint colour, which is what I want.
Later I need to display another view controller over the top, that fills the whole window:
UINavigationController *previewNavigationController = [[UINavigationController alloc]initWithRootViewController:myPreviewViewController];
[owningViewController presentViewController:previewNavigationController animated:YES completion:nil];
The problem I have is that when myPreviewController is displayed, the buttons in the navigation bar are grey. I've tried reinstating the colour on the new navigation controller:
previewNavigationController.navigationBar.tintColor = [UIColor colorWithRed:123/255.0 green:26/255.0 blue:69/255.0 alpha:1];
but without any joy.
How can I get the buttons to have the correct colour? Can I get the new navigation controller to pick up the window tint colour automatically, or do I have to set it explicitly? Is this something to do with presenting the second navigation controller over the top of one that uses UIModalPresentationPageSheet?
Any help much appreciated! Thanks,
Paul
You can set the navigationBar translucent and transparent.
In view Will Appear:
self.navigationController.navigationBar.translucent = NO;
Than create a UIView with frame size of navigationBar (CGRectMake(0,0,self.view.frame.size.height,22) and create buttons on it with color you need.
I know, thats a crutch but should work)
You can change the appearance of the UIBarButtonItem globally so all UINavigationControllers will share the same design:
[[UIBarButtonItem appearance] setTintColor:[UIColor purpleColor]];
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"fontName" size:16.0f],NSFontAttributeName,
nil] forState:UIControlStateNormal];
Additionally you could also change the [UINavigationBar appearance]:
//The setTintColor would tint the < Back button in the NavigationBar
[[UINavigationBar appearance] setTintColor:[UIColor greenColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor blueColor]}];
This code can be add it before presenting the UIViewControllers or simply in the AppDelegate.m.
I have a series of view controllers connected by a root navigation controller. All of the view controllers are table views, except one which is a view controller.
I have a custom navigationBar image which I've applied in the App Delegate and it works great everywhere, except this one view controller. With this, when the segue happens (Push), it almost super imposes the custom background with another image and the result is a darker uinavigationBar.
My code in the App Delegate is:
UIImage *nav = [UIImage imageNamed:#"Purple.png"];
[UINavigationBar appearance] setBackgroundImage:nav forBarMetrics:UIBarMetricsDefault];
Does anyone have any ideas for what I can do to ensure this view controller maintains the correct custom UINavigationBar?
In the viewDidLoad of this view, I've even tried to set this:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"Purple.png"] forBarMetrics:UIBarMetricsDefault];
I also tried this in the viewWillAppear but nothing - still that darker image.
Any thoughts would be greatly appreciated!
you wrote this wrong
UIImage *nav = [UIImage imageNamed:"#Purple.png#];
replace that with this
UIImage *nav = [UIImage imageNamed:#"Purple.png"];