MFMailComposeViewController + UINavigationController iPhone - iphone

I used custom navigation bar within my application,
// appdelegate.h
#implementation UINavigationBar(customNavBar)
- (void)drawRect:(CGRect)rect{
UIImage *navBarImage = [UIImage imageNamed:#"navBar.png"];
[navBarImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
It works fine, but when I use a MFMailComposeViewController for sending an email, it appears with custom navigation bar. Here I don't want the customized navigation bar, I want it to be default.
How do I achieve this?

Here is the example of custom navigation bar for each navigation.
in your case you have to modify that code to your needs.
Change Navigation bar Background image on each navigation

Related

NavigationBar setShadowImage not always working

I'm trying to set a custom shadow image for the navigation bar in my table views, but it's only showing in some views. I've created a super class to set the styles for my table views.
- (void)viewDidLoad
{
[super viewDidLoad];
// Set navigation bar background
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navigationbarbackground.png"] forBarMetrics:UIBarMetricsDefault];
// Set navigation bar shadow imag
[self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:#"navigationbarshadow.png"]];
In the view I see at starting my app, no shadow is showed. But when I touch the [+] button in my navigation bar to open my 'add new item' table view, it does show a shadow.
Could someone point me in the right direction here?
you need to set custom backgroudImage for UINavigationBar, then the shadowImage can work.
The Appearance proxy should work.
Just call it somewhere (e.g. in your AppDelegate) upon startup.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self customizeAppearance];
return YES;
}
- (void) customizeAppearance
{
// Set the background image for *all* UINavigationBars
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigationbarbackground"] forBarMetrics:UIBarMetricsDefault];
// Set the shadow image for *all* UINavigationBars
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"navigationbarshadow.png"]];
//add other appearance stuff here...
}
However if you create a storyboard with multiple UINavigationController's in it and a bunch of segue's pushing navigation controller's you might get a corrupt view controller structure which might be the problem here.
Another possible issue might be the Clip Subviews option of a Navigation Bar somewhere in your nib file or you storyboard. Make sure it is turned off if you want the shadow (image)!
By the way, if you use imageNamed you don't need to include the file extension.
Try this !
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"navbar-iphone.png"]];

iOS - Access Navigation Controller in AppDelegate

I have built my app using Storyboards. I have a UITabBarController and a NavigationController embedded it that.
I am trying to customise my app with tint colors and I have managed to set a custom color for the Tabbar. I was able to access the tabbar from the rootViewController, however I cannot get access to the navigation bar. Is there anyway that I can easily access this?
Sample of my AppDelegate code is below.
Thanks
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.tabBarController = (UITabBarController*)self.window.rootViewController;
UITabBar *tabBar = self.tabBarController.tabBar;
[tabBar setSelectedImageTintColor:[UIColor greenColor]];
tabBar.tintColor = [UIColor colorWithRed:0.4 green:0.522 blue:0.129 alpha:1];
//This bit doesnt work
UINavigationController *navCon = self.tabBarController.navigationController;
navCon.navigationBar.tintColor = [UIColor colorWithRed:0.384 green:0.259 blue:0.161 alpha:1];
return YES;
}
If you're using navigation controllers, would expect them to embed the view controllers that are managed by the tab bar controller.
Try NSLog(#"%#", [self.tabBarController viewControllers]); and see whether it lists navigation controllers or your custom controller. For example, I created two view controllers that are triggered by my tab bar, one with and one without navigation and the above line shows:
(
"<UINavigationController: 0x6a35f20>",
"<SecondViewController: 0x6a39500>" )
So, I could get mine as [[self.tabBarController viewControllers] objectAtIndex:0].
Assuming you did no work with IBOutlet's, I suggest you maybe use them along with the XIB file. For example, In the header file:
IBOutlet UINavigationBar *aNavBar;
And then in the .m, switch:
UINavigationController *navCon = self.tabBarController.navigationController;
navCon.navigationBar.tintColor = [UIColor colorWithRed:0.384 green:0.259 blue:0.161 alpha:1];
With:
[aNavBar setTintColor:[UIColor colorWithRed:0.384 green:0.259 blue:0.161 alpha:1]];
And there you go! I made this quick, so tell me if it works or not.
-Justin A.

Changing the navigationBar on pushViewController:

In my tabBar based app I have subclassed the UINavigationBar. Let's say I have three of them: BlueNavBar, BlackNavBar and RedNavBar. It looks something like this:
//BlueNavBar.m
- (void)drawRect:(CGRect)rect {
self.tintColor = [UIColor colorWithRed:65.0f/255.0f green:(156.0f/255.0f) blue:(215.0f/255.0f) alpha:1.0];
UIImage *image = [[UIImage imageNamed:#"blueNavBar.png"]retain];
[image drawInRect:rect];
[image release];
}
I've assigned the subclassed navigationbar for each tab with Interface Builder. That is working great, no problems there.
In some viewControllers however i want to change the navigationBar during "pushViewController". Let's say I want to change the current navigationbar (which is for e.g. BlueNavBar) to the RedNavBar. How can I do this programmatically, without Interface Builder?
It depends on how you've designed the view controller classes themselves. One way to design what you need would be to set the navigation bar type (i.e. colour) when you create the view controller, before you push it on the stack. Something like:
SomeViewController* someViewController = [[SomeViewController alloc] initWithNibName:#"SomeView" bundle:nil];
someViewController.navigationBarStyle = NBStyleRed; // NBStyleRed defined as an enum somewhere
[self.navigationController pushViewController:someViewController animated:YES];
[someViewController release];
The setter method for navigationBarStyle would then (re)create an appropriately-coloured navigation bar for the view controller.

Change iPhone navigation bar's height

My client can't read iPhone's default fonts, the size is too small. I have an application with a navigation bar and I need to make everything in it bigger, for example, the font's size.
IB doesn't seem to allow this... any help?
Many thanks!
Update: today (2012) there is a much bigger tendency towards custom UIs, so I would say the answer below is way too harsh. There is still no supported way of customizing height, though, but you can certainly derive from UINavigationBar and override some sizing methods. This probably will not get you rejected (although it is still a grey area, just something Apple will probably overlook today).
Once you get the size you want, you can use iOS 5 customization APIs to add the custom background image (see WWDC 2011 Session 114 - Customizing the Appearance of UIKit Controls).
Original answer from 2009:
This is generally impossible.
What's more, I believe making the navigation bar taller is a violation
of Apple Human Interface Guidelines, and your application may be
rejected from the App Store because of it. Please make sure your
client understands this risk before proceeding.
(Pointing out rejection risks is usually a good way to convince
clients against making nonsense decisions.)
Many of the answers here are incorrect, or incomplete, so I wanted to add my answer here in the hope that it might enlighten some.
First off, there is nothing wrong with changing the height of the navigation bar. People commenting saying its not allowed, or goes against the guidelines are simply misunderstanding those guidelines.
The ability to adjust or alter the default navigation bar that's used inside a UINavigationController has been part of the SDK since iOS 5.
- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass NS_AVAILABLE_IOS(5_0);
The easiest way to change the height of the status bar is to use this method when initialising your navigation controller, passing in your custom UINavigationBar sub-class.
TestViewController *t = [[TestViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:[UIToolbar class]];
[nav setViewControllers:#[t]];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
Where an example of such a custom UINavigationBar class could look like:
#interface MyNavigationBar : UINavigationBar
#end
#implementation MyNavigationBar
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize s = [super sizeThatFits:size];
s.height = 90; // Or some other height
return s;
}
#end
If you decided to just change the font size in the navigation bar, you can do this (usually in your UIViewController's viewDidLoad method):
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[titleLabel setBackgroundColor:[UIColor clearColor]];
// here's where you can customize the font size
[titleLabel setFont:[UIFont boldSystemFontOfSize:18.0]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setText:self.title];
[titleLabel sizeToFit];
[titleLabel setCenter:[self.navigationItem.titleView center]];
[self.navigationItem setTitleView:titleLabel];
[titleLabel release];
By subclassing you can achieve that and still support iOS 3+:
Complete example:
#import <UIKit/UIKit.h>
#interface ASNavigationBar : UINavigationBar
#property (nonatomic , retain) UIImage *backgroundImage;
#end
And implementation:
#import "ASNavigationBar.h"
#implementation ASNavigationBar
#synthesize backgroundImage = _backgroundImage;
-(void) setBackgroundImage:(UIImage *)backgroundImage
{
if (_backgroundImage != backgroundImage)
{
[_backgroundImage release];
_backgroundImage = [backgroundImage retain];
[self setNeedsDisplay];
}
}
-(void) drawRect:(CGRect)rect
{
// This is how the custom BG image is actually drawn
[self.backgroundImage drawInRect:rect];
}
- (CGSize)sizeThatFits:(CGSize)size
{
// This is how you set the custom size of your UINavigationBar
CGRect frame = [UIScreen mainScreen].applicationFrame;
CGSize newSize = CGSizeMake(frame.size.width , self.backgroundImage.size.height);
return newSize;
}
#end
Important notes:
If the background image is with transparent areas, you have to set its barStyle property to "translucent" or the transparent areas will be black.
If you have a NavigationBar taller than 44 points, you have to take into account that the position of the BarButtonItems might not be correct. They all will be anchored to the bottom of the bar. you can fix that by overriding layoutSubviews and change their origin.y value.
You should not change the height of the navigation bar. From Apple Programing Guide on View Controller:
Customizing the Navigation Bar Appearance
In a navigation interface, a navigation controller owns its UINavigationBar object and is responsible for managing it. It is not permissible to change the navigation bar object or modify its bounds, frame, or alpha values directly. However, there are a few properties that it is permissible to modify, including the following:
● barStyle property
● translucent property
● tintColor property
(taken from Apple: https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html)
UPDATE -- IOS 7 --- still only the available properties can be changed but below is a great tutorial on how to achieve flexibility in the navigation bar http://www.appcoda.com/customize-navigation-status-bar-ios-7/
To add to Skela's answer:
If you initiate your navigation controller in the Storyboard you can change the class of your UINavigationBar in the storyboard to your custom navbar.
and then implement the change height in the class
#interface MyNavigationBar : UINavigationBar
#end
#implementation SwitchAssessmentNavigationBar
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize s = [super sizeThatFits:size];
s.height = 200; // Or some other height
return s;
}
#end

change color of navigation bar

How can I change the color of navigation bar from its default blue color?
Thanks.
The UINavigationBar class has a UIColor *tintColor property that you can change in code.
Alternately this property is also exposed in the InterfaceBuilder UI design tool.
Assuming you have added the navigation bar programmatically and not in Interface Builder, just put this in your viewDidLoad method.
self.navigationController.navigationBar.tintColor = [UIColor grayColor];
TintColor property doesn't affect default subview of navigation bar as bottom border and shadow. Sometimes it's useful to override layout of navigation bar at all.
Despite navigationBar is read-only property for UINavigationController you can avoid
this restriction by "setValue:forKey:". This method was approved on 5 applications successfully submitted to AppStore.
You can subclass UINavigationBar and change drawRect: method as you want.
For example,
#implementation CustomNavigationBar
- (void) drawRect:(CGRect)rect
{
[super drawRect:rect];
UIImage *backgroundImage = ImageFromColor(WANTED_COLOR);
[backgroundImage drawInRect:rect];
}
After you can subclass UINavigationController and change initWithRootViewController:
- (id) initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithRootViewController:rootViewController];
if (self)
{
CustomNavigationBar *navBar = [CustomNavigationBar new];
[self setValue:navBar forKey:#"navigationBar"];
}
return self;
}
Also you can vary this approach by making Category for UINavigationController and implementing method swizzling for initWithRootViewController:
P.S. Yesterday my new app appeared at AppStore without any problem with this approach.