I want remove border of navigation bar. I am using this code in AppDelegate.m
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init]
forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
and it works well. It removes border of navigation bar in every view controller.
But i have to remove it for few view controllers.
By placing this code in viewWillAppear of particular view controller, its not working.
Does anyone knows how to do it?
You should set the background image and shadow image of the navigation bar in your select navigations bars and not to use the appearance method. e.g.:
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
Related
I want to have a translucent navigation bar and display view or controller under it. Through the navigation bar, user can see the content display on the controller.But it always display from y=64 in iOS8. Do you know how to do it?
Put this code in applicationDidFinishLaunchingWithOption
//visible viewcontroller
[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//status bar
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
//One line pixel
[[UINavigationBar appearance] setShadowImage:[UIImage new]];
I am currently attempting to open up the iOS mail all to create a draft with an attachment generated from my app. Here is a code snippet:
NSURL *fileLocation = [NSURL fileURLWithPath:self.site.dataFileLoc];
if(fileLocation) {
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:fileLocation];
[self.documentController setDelegate:self];
[self.documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
}
However, what I am finding is that the navigation bar colours are completely off. In the AppDelegate, I am currently using these lines to set the global navigation bar appearance:
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navbarBackground"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearanceWhenContainedIn:[UIViewController class], nil] setShadowImage:[UIImage imageNamed:#"shadow"]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
Unfortunately, the title of the mail view controller that appears is still black, despite my attempt to set the bar style to black (which should make the title white in return) and blue bar buttons. It does, however, seem to work fine for the "presentPreviewAnimated" view controller. How can I make the navigation bar of the mail view controller consistent?
Are there any nibs associated??
I usually try to avoid the desired level of UI customization (navigation bar styles, colors, custom background w/image) because drawing UI at runtime (unless dynamically required) generally gets me in trouble...
Why not mock it up in a .nib file and make use of the fancy-dance-y tools in Xcode?? Can be therapeutic to quickly obtain the desired grain of detail without having to hit run...
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.
Earlier I used a navigationcontroller with this code :
self.navigationItem.title = #"News";
Now I have the problem that I no longer needed the navigationcontroller as I am using a page controller for navigation. Now I added a navigationbar however it doesn't change the title with this code anymore.
Also how can I change the background color?
IN iOS 7 use write following code in didFinishLaunchingWithOptions
if ([[UINavigationBar class] respondsToSelector:#selector(appearance)])
{
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed: 4.0/255.0 green:173.0/255.0 blue:214.0/255.0 alpha:1.0f ]]; //// change background color of navigationBar
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; /// set backButton color of navigation bar
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}]; // set title color
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal]; /// set all barButton item color
self.navController.navigationBar.translucent = NO;// set translucent NO
}
Use as per your requirement.
So there is some magic happening when you use a navigation controller that you don't see and now that you've made the switch to justing using a navigation bar you lose that magic.
Navigation bars are responsible for managing and presenting UNavigationItems, and as such hold an array of them. A NavigationItem holds things like left and right buttons, and the title or titleView. When your navigation controller pushes a new controller, it creates a brand new UINavigation item then links it to your new view controller, and then pushes that onto navigation bars stack of nav items. That's why from within your view controller you set the title with self.navigationItem.title instead of referencing the navbar.
Basically you have to manage the bar and nav items yourself now. This should about do it for you:
_navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, yOffset, CGRectGetHeight(size), height)];
_navItem = [[UINavigationItem alloc] initWithTitle:#"My navbar title"];
[_navBar setItems:#[_navItem]];
[self.view addSubview:_navBar];
Of course you'll have to manage the size a bit differently as my example comes from an app where it's being used in landscape mode.
I am working with themes in my app. Each theme comes with a custom navigation bar. As a default, I have applied a custom navigation bar in my AppDelegate:
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
UIImage *navBackgroundImage = [UIImage imageNamed:#"purplynav.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
If I go to a specific Table View Controller in my app and in the viewWillAppear, I put:
if ([self.selectedTheme isEqualToString:#"Blur"])
{
UIImage *navBackgroundImage = [UIImage imageNamed:#"pastelpinknav.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
}
The "pastelpinknav" suddenly gets applied to every single navigation bar in my app, even though I'm only setting it in this one Table View Controller.
How do I set it so that the navigation bar only applies to this one view controller when set, but is still set to default in the App Delegate when a theme is not set?
Thanks in advance!
You have to change again the navigationBar image in viewWillDisappear
UIImage *navBackgroundImage = [UIImage imageNamed:#"purplynav.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
Because you are using [UINavigationBar appearance] it will commonly changes the UINavigationBar appearance.
From your view controller call:
[[UINavigationBar appearanceWhenContainedIn:[self class], nil] setBackgroundImage:navBackgroundImage
forBarMetrics:UIBarMetricsDefault];
Explenation:
This will change the foo color of the entire app:
[[UINavigationBar appearance] setFooColor:#"blue"]
In order to change appearance of specific container (i.e your view controller), you need to implement UIAppearanceContainer and then call (UIViewController already implements it):
[UINavigationBar appearanceWhenContainedIn:(Class<UIAppearanceContainer>), ..., nil]