Adding images and text together on navigation bar on left side - ios

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:self.viewController];
// CHANGE COLOR TO BLACK
nav.navigationBar.tintColor = [UIColor blackColor];
// ADDING IMAGE TO NAVIGATION BAR
UIImage *image = [UIImage imageNamed:#"logo_36.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[nav.navigationBar.topItem setTitleView:imageView];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
I have tried this code in appdelegate class because i want it throughout the whole project, it works for displaying image, but it shows in centre also i need text which i couldn't display.
Please could someone suggest how can i display both image and text on left side of navigation bar in iOS

Why you are setting imageview to navigation topItem titleview
Replace
[nav.navigationBar.topItem setTitleView:imageView];
with nav.navigationbar.topItem.leftBarButtonItem:imageview
and set `[nav.navigationBar.topItem setTitle:#"text you want"];
UIImage* image3 = [UIImage imageNamed:#"mail-48_24.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(sendmail)
forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.leftBarButtonItem=mailbutton;
[someButton release];

Related

ios tabBarControl, navigationcontroller title and button editing

I've been using a standard TabBarController and top NavigationBar within my app. The tabBar loads in the appDelegate, initializes the navigationBar to use an image instead of center title, and this exists for all four tabBar views. Occasionally I'll push a new view onto the view controllers and that works well enough.
What I'd like to do is be able to change the NavigationBar for each of the 4 tabBar view controllers, when they are opened. I'm having trouble doing this. My initial navigationBar has an image, loaded in the appDelegate. My questions from here are:
Should each viewController generally create a new navigationBar from scratch, per its needs, in the respective viewWillAppear method?
What navigationController and navigationBar should be editted-- always the appDelegate's navigationController, the tabBarController.navigationController, or simply self.navigationController in each view? (generally, most editing most of these is not working for me, causes no changes to occur)
If I override the standard title (which is usually the tabBar's current view's title) with the imageView, and want another tabBar view to use the standard title, how should I remove the titleView image, and reset back to text? And viceVersa, dependent on view? This is what I'm really trying to accomplish unsuccessfully.
I guess I'm looking for standard practice in managing the navigationBar per view, and changing it per tabBarItem.
// in appDelegate
- (void)initNav {
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate = self;
self.tabBarController.navigationController.view.backgroundColor = [UIColor whiteColor];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.firstViewController,
self.secondViewController,
self.thirdViewController,
self.forthViewController,
nil];
[self.window addSubview:self.tabBarController.view];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.tabBarController];
self.navigationController.navigationBarHidden = NO;
// customize background color of nav bar to "blue"ish color
self.navigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:#"00A3E1"];
self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:#"00A3E1"];
self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:#"00A3E1"];
[self createNavs];
}
// also in appDelegate
- (void)createNavs {
// white text when present
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],
UITextAttributeTextColor,
[UIColor clearColor],
UITextAttributeTextShadowColor, nil];
[[UIBarButtonItem appearance] setTitleTextAttributes: attributes
forState: UIControlStateNormal];
AppDelegate *delegateRef = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[self.navigationController.navigationBar setTranslucent:NO];
// setup left button (currently unused -- no left button)
/*
/*
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"glyphicons_049_star.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(starClick:) forControlEvents:UIControlEventTouchDown];
[button setFrame:CGRectMake(0, 0, 25, 25)];
self.navigationController.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
*/
// setup logo in center
self.tabBarController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"synced_top_logo.png"]];
// setup right button (currently unused -- no right button)
/*
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
rightButton.title = #"edit";
[rightButton setTarget:self ];
[rightButton setAction:#selector(editClick:)];
self.navigationController.navigationBar.topItem.rightBarButtonItem = rightButton;
*/
[[self navigationController] setNavigationBarHidden:NO animated:YES];
[[delegateRef navigationController] setNavigationBarHidden:NO animated:YES];
}
EDIT:
This is a simplified version of the solution posted below that was helpful, and allowed the customization that was needed. Each view controller customizations its navigationbar independently from this. It was probably how it should have been done from the start.
tabBarController.viewControllers = [NSArray arrayWithObjects:[[UINavigationController alloc] initWithRootViewController:self.firstViewController],
[[UINavigationController alloc] initWithRootViewController:self.secondViewController],
[[UINavigationController alloc] initWithRootViewController:self.thirdViewController],
[[UINavigationController alloc] initWithRootViewController:self.forthViewController],
nil];
This maybe what you are needing:
- (void)initTabbar {
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate = self;
/*
You want each of your UIViewControllers to be wrapped in a UINavigationController. Then put each of those UINavigationControllers in a UITabBarController
*/
//You don't need to hang on to this becuase the proceeding UINavigationController will handle it
FirstViewController *firstViewController = [[FirstViewController alloc] ...];
//You'll need to declare this in your header
self.firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
//Second view allocation
SecondViewController *secondViewController = [[SecondViewController alloc] ...];
self.secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
//Third view allocation
ThirdViewController *thirdViewController = [[ThirdViewController alloc] ...];
self.thirdNavigationController = [[UINavigationController alloc] initWithRootViewController:thirdViewController];
//Now you add each of the UINavigationControllers (which is a subclass of UIViewController) to the UITabBarController.
self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.firstNavigationController,
self.secondNavigationController,
self.thirdNavigationController,
nil];
[self.window addSubview:self.tabBarController.view];
[self createNavs];
}
//This is more of a 'formatNavs' now
- (void)createNavs {
//Now you can customize each of the UINavigationController's UINavigationBars seperatly
self.firstNavigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:#"00A3E1"];
self.firstNavigationController.navigationBar.tintColor = [UIColor colorWithHexString:#"00A3E1"];
self.firstNavigationController.navigationBar.barTintColor = [UIColor colorWithHexString:#"00A3E1"];
self.secondNavigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:#"...."];
self.secondNavigationController.navigationBar.tintColor = [UIColor colorWithHexString:#"...."];
self.secondNavigationController.navigationBar.barTintColor = [UIColor colorWithHexString:#"...."];
self.thirdNavigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:#"...."];
self.thirdNavigationController.navigationBar.tintColor = [UIColor colorWithHexString:#"...."];
self.thirdNavigationController.navigationBar.barTintColor = [UIColor colorWithHexString:#"...."];
}

Add navigation bar on a view controller

i am new on iOS and i wanna add a navigation bar on my view controller with 2 buttons back on left and subscribe on right. ive got no clue how to do it..till now i have just added a nav bar from interface builder, created a (strong)refrnce for it in .h file and did following coding.
navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, 1026, 50)];
[navBar setTintColor:[UIColor clearColor]];
[navBar setBackgroundColor:[UIColor redColor]];
[navBar setDelegate:self];
[self.view addSubview:navBar];
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:#"subscribe" style:UIBarButtonItemStyleBordered target:self action:#selector(editBotton)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.tintColor =[UIColor colorWithWhite:0.305f alpha:0.0f];
self.navigationItem.rightBarButtonItem = bi1;
but nothings happeing.. please help
You can add in AppDelegate,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone"
                                                             bundle: nil];
 
    SampleViewController *mainViewController = (SampleViewController*)[mainStoryboard
                                                       instantiateViewControllerWithIdentifier: #"SampleViewController"];
 
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setRootViewController:navigationController];
    [self.window setBackgroundColor:[UIColor whiteColor]];
    [self.window makeKeyAndVisible];
 
    return YES;
}
I think you have to learn the basic concepts about UINavigationController. You can learn from the below tutorials:
http://simplecode.me/2011/09/04/an-introduction-to-uinavigationcontroller/
http://www.ralfebert.de/archive/ios/tutorial_iosdev/navigationcontroller/
http://bharanijayasuri.wordpress.com/2012/12/19/simple-uinavigationcontroller-tutorial-2/

UINavigationController Navigation Bar Image is hiding other backbuttons and custom images?

- (UINavigationController *)navigationController {
nav = [[UINavigationController alloc]
initWithRootViewController:[self demoController]];
UIImage *image;
// If Iphone/iPod Touch
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
// If iPhone 5 or new iPod Touch
if([UIScreen mainScreen].bounds.size.height == 568){
image = [UIImage imageNamed:#"nav.png"];
} else{
// Regular iPhone
image = [UIImage imageNamed:#"nav.png"];
}
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[nav.navigationBar addSubview:imageView];
return nav;
}
As I try to add leftBarButtonItem as
- (UIBarButtonItem *)leftMenuBarButtonItem {
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[backButton setBackgroundImage:[UIImage imageNamed:#"main_menu.png"] forState:UIControlStateNormal];
UIBarButtonItem *barBackButtonItem = [[UIBarButtonItem alloc] initWithCustomView: backButton];
[backButton addTarget:self action:#selector(leftSideMenuButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
return barBackButtonItem;
}
It doesn't display main_menu.png on navigation, but if comment this code
// [nav.navigationBar addSubview:imageView];
it will show main_menu.png, what is issue, why nav.png is now showing other images, it is added as SubView, so what should I do to display others left or right bar button items, over nav.png.
Yo shouldn't be adding the image to an image view and adding that as a subviews. Instead you should be calling setBackgroundImage:forBarMetrics: or using the appearance delegate to set the background image for the navigation bar.

How to make NavigationBar rotate with device

I have added a navigation bar programmatically to my app as follows:
//Setup Navigationbar:
navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, [[UIScreen mainScreen ] bounds].size.width, 44)];
UINavigationItem *navigItem = [[UINavigationItem alloc] initWithTitle:#"Navigation Title"];
//Navigationbar left items:
UIButton *fileButton = [UIButton buttonWithType:UIButtonTypeCustom];
[fileButton addTarget:self action:#selector(loadFile:) forControlEvents:UIControlEventTouchUpInside];
[fileButton setImage:[UIImage imageNamed:#"menuicon.png"] forState:UIControlStateNormal];
fileButton.frame = CGRectMake(0, 0, 24, 24);
fileButton.backgroundColor=[UIColor clearColor];
newItem = [[UIBarButtonItem alloc] initWithCustomView:fileButton];
navBar.items = [NSArray arrayWithObjects: navigItem,nil];
//NavigationBar appearance:
[navBar setBackgroundImage:[UIImage imageNamed:#"greenbar.png"] forBarMetrics:UIBarMetricsDefault];
[self.window addSubview: navBar];
[self setTitle:#"Empty"];
Problem is i can't get it to rotate with the device, As you can see im adding the bar to the windows of my AppDelegate. How do i make it follow the rotation and autoresize?
To enable the rotation of the navbar it needs to be added to a view that reacts on the rotation of the device. I fixed the issue by switching from [self.window addSubview: navBar]; to [self.window.rootViewController.view addSubview: navBar];

issue in setting image of navigation bar while presentModalViewController

this is how i set image in my first class in viewDidLoad which is tableView
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]) {
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"header.png"] forBarMetrics:UIBarMetricsDefault];
}
now this how i go to detail view
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
in detail view's bar the image which i have set for navigation bar comes automatically there and everything works perfect
now my problem is that pushViewController is working perfectly images are getting displayed
but i get default image in presentModelViewController this is how i use it
- (void) modelappear
{
if (self.testModalViewController == nil)
{
TestModalViewController *temp = [[TestModalViewController alloc] initWithNibName:#"TestModalViewController" bundle:[NSBundle mainBundle]];
self.testModalViewController = temp;
[temp release];
}
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.testModalViewController];
[self presentModalViewController:nav animated:YES];
}
Note i just set buttons in inner hierarchy views
Can you please tell me what am i doing wrong? please give explanation with answer thank you so much
try like this ,
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
dvController.topViewController.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIIMage imageNamed:#"name.png"]];
[self presentModalViewController:nav animated:YES];
UIImage *image = [UIImage imageNamed:#"header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
UIImage *image = [UIImage imageNamed:#"header.png"];
UIImageView *imageViewe = [[UIImageView alloc] initWithImage:image];
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 3, 150, 40)];
[tmpTitleLabel setFont:[UIFont boldSystemFontOfSize:18]];
tmpTitleLabel.text = #"Add Manually";
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
[newView addSubview:imageViewe];
[newView addSubview:tmpTitleLabel];
[self.navigationController.navigationBar addSubview:newView];

Resources