2

I have added Navigation bar and Navigation Item in xib. Now I need to add image to the left of Navigation bar, but its not getting added. This is the code I'm working on:

- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed: @"i_launcher.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
    self.navigationItem.titleView = imageView;
    [imageView release];
}

I can't understand why the image is not getting added. How can I add it?

shellco
  • 536
  • 1
  • 6
  • 25
Sindhia
  • 411
  • 3
  • 12
  • 33
  • check my answer http://stackoverflow.com/questions/13488710/how-to-set-a-picture-programmatically-in-a-navbar#comment18517078_13488710 – Rajneesh071 Nov 28 '12 at 05:47
  • possible duplicate of [how to display an image in the navigation bar of an iPhone application?](http://stackoverflow.com/questions/844416/how-to-display-an-image-in-the-navigation-bar-of-an-iphone-application) – Hemant Singh Rathore Aug 08 '15 at 09:51

9 Answers9

8

Looks like this is an old question with an accepted answer. However, I wanted to add:

In at least Xcode 5, if you are using storyboards then you can add the image as the navigationBar's title by dragging an empty view into the title area, and then placing the ImageView inside the empty view.

The following screenshot should give you an idea of how it looks: Storyboard screenshot depicting imageView inside of empty view inside the navigationBar.titleView

Aron C
  • 838
  • 1
  • 10
  • 17
6
@implementation UINavigationBar (CustomImage) -(void)drawRect:(CGRect)rect { 

     CGRect currentRect = CGRectMake(0,0,100,45); 
     UIImage *image = [UIImage imageNamed:@"ic_launcher.png"]; 
     [image drawInRect:currentRect]; 
} 
@end 

UPDATE

just add this code in your viewDidLoad: method..

self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage2.jpg"]]];    
self.navigationItem.rightBarButtonItem = item; 

and also if you want to direct set image in background of NavigationBar then use bellow line...

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"i_launcher.png"] forBarMetrics:UIBarMetricsDefault];

also see this Link how-to-add-a-customized-image-button-to-uinavigationbar-in-iphone-sdk

i hope this help you...

Community
  • 1
  • 1
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • @Esha see this link how-to-add-image-in-uinavigationbar-in-iphone-app – Paras Joshi Nov 28 '12 at 06:01
  • I have added it in ViewDidLoad .But I m getting errors:"Unexpected @ in program.. and so on..Where should I add it ? – Sindhia Nov 28 '12 at 06:02
  • just above of your @implementation yourViewController class, out of your class code – Paras Joshi Nov 28 '12 at 06:05
  • @Esha create image with your requirement and add this image as a background of your navigationbar and set background image with my second code try this... – Paras Joshi Nov 28 '12 at 06:07
  • I got ur code given this to be working :@implementation UINavigationBar (CustomImage) -(void)drawRect:(CGRect)rect { CGRect currentRect = CGRectMake(0,0,100,45); UIImage *image = [UIImage imageNamed:@"ic_launcher.png"]; [image drawInRect:currentRect]; } @end . – Sindhia Nov 28 '12 at 06:27
  • but a little bit problem .When I go with this code it changes the blue background colour of navigation bar to white.Y is it so?What I need is I need the background of image to be white.As I have said I have to add image to left side and in center title.I need the background of left over part after image to be in blue colour.How can I do it ? – Sindhia Nov 28 '12 at 06:29
  • @Esha just add this code [self.navigationController.navigationBar setTintColor:[UIColor blueColor]]; – Paras Joshi Nov 28 '12 at 06:32
  • @Esha also try second option with update its also work fine otherwise set the titntcolor of navigationbar – Paras Joshi Nov 28 '12 at 06:37
  • I added the code [self.navigationController.navigationBar setTintColor:[UIColor blueColor]]; but still background colour did n't change – Sindhia Nov 28 '12 at 06:37
  • when i try to add ur updated code image itself is not getting displayed – Sindhia Nov 28 '12 at 06:38
  • first comment my default code which you implement and try with this otherwise create one image with your whole navigationbar which you want and then add background image to navigationbar – Paras Joshi Nov 28 '12 at 06:45
  • No still I dont get it.I just got with @implementation code only.So tell me how can I change only the background part with image to be in white colour. – Sindhia Nov 28 '12 at 06:55
  • you try this self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]]; UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage2.jpg"]]]; self.navigationItem.rightBarButtonItem = item; ?? – Paras Joshi Nov 28 '12 at 07:05
  • no luck.I think it is bcoz I added navigation bar programatically.Can 't I change only the background part of image to be in white colour with ur code? – Sindhia Nov 28 '12 at 07:15
  • @Esha [yourNaviGationBar setBackgroundImage:[UIImage imageNamed:@"i_launcher.png"] forBarMetrics:UIBarMetricsDefault]; try this with also try this [yourNavigationBar setTintColor:[UIColor blueColor]]; and just change some code its working all just setting with your requirement .. – Paras Joshi Nov 28 '12 at 07:21
  • see I have created a ViewBased application.In appDelegate.m I added this - – Sindhia Nov 28 '12 at 07:43
  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {ViewController *first=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil]; Login *second=[[Login alloc]initWithNibName:@"Login" bundle:nil]; second.title=@"Login";NSArray *viewArray=[[NSArray alloc] initWithObjects: first,second,nil];tabBarController=[[UITabBarController alloc] init];[tabBarController setViewControllers:viewArray animated:NO];[self.window addSubview:tabBarController.view];[self.window makeKeyAndVisible]; [viewArray release];return YES; } – Sindhia Nov 28 '12 at 07:43
  • To ViewController I added a ScrollView and a navigation bar to a ScrollView through xib.So I dont have any navigation bar code – Sindhia Nov 28 '12 at 07:44
  • you give the outlet or referacnce to the navigationbar?? – Paras Joshi Nov 28 '12 at 08:34
  • one more doubt.Suppose I have added another navigation bar in same viewcontroller then I should not get image for it .How can I get it ?by using tag? – Sindhia Nov 28 '12 at 09:28
  • yes just give different name and hide and show the navigation bar bt why you add two navbar?? – Paras Joshi Nov 28 '12 at 09:38
  • when i give different name aso im getting the image attached to another navigation bar also – Sindhia Nov 28 '12 at 09:45
  • i think you give the image with self.navigatio... but insted of use with navigationbar name – Paras Joshi Nov 28 '12 at 09:50
  • one more things y is the image getting added to all the navigation controllers of all ViewControllers when I add it only to a specific ViewController?@implementation UINavigationBar (CustomImage) -(void)drawRect:(CGRect)rect { CGRect currentRect = CGRectMake(0,0,100,45); UIImage *image = [UIImage imageNamed:@"ic_launcher.png"]; [image drawInRect:currentRect]; } @end – Sindhia Nov 28 '12 at 10:06
  • hey its happen bcoz another viewcontroller are child control of this class so its the same navigationcontroller, if u want to change it then give the another view or change the style of navigationcontrol thats it.. and if this answer useful to u then dont forget to accept answer or upvote it thank you.. :) and if u want to use this navbar view in same view then in another view set another view or use this self.navigationController.navigationBar.barStyle = UIBarStyleBlack; or another style.. i hope u understand :) – Paras Joshi Nov 28 '12 at 10:10
4
- (void)viewDidLoad

    {


       UIImage *image = [UIImage imageNamed:@"Your Image"];
       UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
       imageView.frame = CGRectMake(5, 10, 72, 19);
       [self.navViewController.navigationBar addSubview:imageView];
   }
Bhoopendra
  • 156
  • 4
1

If you want to set your image at particular point in navigation bar then you can make a view and then add your image inside the view, adjust your image frame and then add this view to your navBar.

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Default.png"]];
[image setFrame:CGRectMake(0, 0, 44, 44)];
[view addSubview:image];
[self.navigationController.navigationBar addSubview:view];

If you are adding NavBar from xib then just make IBOutlet for your UINavigationBar connect it in xib then

[myNavigationBar addSubview:view];
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
  • No I didnt get it . As I have said I added navigation bar through xib and so these codes are not getting recognized.... – Sindhia Nov 28 '12 at 07:10
1

For Swift 4:

override func viewDidAppear(_ animated: Bool) {
        // 1
        let nav = self.navigationController?.navigationBar

        // 2
        nav?.barStyle = UIBarStyle.black
        nav?.tintColor = UIColor.yellow

        // 3
        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        imageView.contentMode = .scaleAspectFit

        // 4
        let image = UIImage(named: "Apple_Swift_Logo")
        imageView.image = image

        // 5
        navigationItem.titleView = imageView
    }
Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43
0

Try to set the imageView.frame also please cross check the image initailized properly

UIImage *image = [UIImage imageNamed: @"i_launcher.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame.origin.x = 30.0; // You will have to find suitable values for the origin
imageView.frame.origin.y = 5.0;
self.navigationItem.titleView = imageView;
[imageView release];
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • getting error :Expression is not assignable at this line imageView.frame.origin.x = 30.0; – Sindhia Nov 28 '12 at 05:53
  • Did like this UIImage *image = [UIImage imageNamed: @"i_launcher.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage: image]; imageView.frame =CGRectMake(0,0,50,50);// You will have to find suitable values for the origin self.navigationItem.titleView = imageView; [imageView release]; Stil couldnot c d image added ? – Sindhia Nov 28 '12 at 06:07
0

Your code looks ok. But I don't know why you need to add navigation bar and item in xib. If you create your own view controller derived from UIViewController, it has contained navigation bar. You may try to remove yourself added navigation bar and item from xib and see what's happen.

If you want to show nav bar on the first view of a view-based application, you need to modify your application class code as below:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];

    // REMOVE THIS LINE !!!!!!!!!!!!!
    //self.window.rootViewController = self.viewController;

    // ADD BELOW TWO LINES !!!!!!!!!!!!!!!!!!!!!!!
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = nav;

    [self.window makeKeyAndVisible];
    return YES;
}

I just did a test like the following:

  1. Create a new single-view based project.
  2. Run it without any modification. It will show an empty screen.
  3. Modify didFinishLaunchingWithOptions() as above in the application.m file.
  4. In the viewcontroller.m, add the following lines.

    -(void)viewDidLoad { [super viewDidLoad]; UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; view.image = [UIImage imageNamed:@"info.png"]; self.navigationItem.titleView = view; }

  5. Now run again. You will see a nav bar with a image as title.

TieDad
  • 9,143
  • 5
  • 32
  • 58
  • but i dont see the navigation bar added automatically on running viewbased application unless I add it externally.So I have added externally to my View Controller – Sindhia Nov 28 '12 at 05:57
  • I have added code in answer. I just tested those code, it works. – TieDad Nov 28 '12 at 06:56
  • I added ur code and then in view controller added my code.Still it doesnot show any navigation bar to me – Sindhia Nov 28 '12 at 07:05
  • Did you remove yourself-added nav bar from xib? – TieDad Nov 28 '12 at 07:07
  • yes i removed it .it is a view based application.I added ur code in Appdelegate.m – Sindhia Nov 28 '12 at 07:11
  • I just did a test, it works. Please see my answer update. You may try it also. – TieDad Nov 28 '12 at 07:14
  • I my view I have a scrollview and I m adding navigation bar to it thrugh xib – Sindhia Nov 28 '12 at 07:21
  • I dont have single-view based project in xcode 4.1.So gone with view based project.but if I did as u have said then I m getting an empty white view – Sindhia Nov 28 '12 at 07:31
  • My xcode is latest. I have no way to test on xcode 4.1. But I don't believe it's xcode difference. View-based project is same as single-view based project, apple just renamed since xcode 4.2. – TieDad Nov 28 '12 at 07:37
0

I hope this will help you sure. if not work mean check your image name is correct.

UIImage *yourimage = [UIImage imageNamed: @"i_launcher.png"];

UIButton *buttonBarButton = [ UIButton buttonWithType:UIButtonTypeCustom];
buttonBarButton.frame = CGRectMake(0, 0,yourimage.size.width,yourimage.size.height);
[buttonBarButton setBackgroundImage:yourimage forState:UIControlStateNormal];
[buttonBarButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *buttonBarButtonItem = [[[UIBarButtonItem alloc]initWithCustomView:buttonBarButton] autorelease];
self.navigationItem.leftBarButtonItem = buttonBarButtonItem;

if you wish add action means :

[buttonBarButton addTarget:self action:@selector(your function) forControlEvents:UIControlEventTouchUpInside];
Bista
  • 7,869
  • 3
  • 27
  • 55
0

To hide / show the image between movements to another controller do this. Adjust the CGRectMake dimensions accordingly.

Add this to your header file:

@property (weak, nonatomic) UIImageView *navBarLogo;

Add this to your .m controller file:

- (void)viewWillAppear:(BOOL)animated {

    UIImage *image = [UIImage imageNamed:@"image_name.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(53, 7, 210, 30);

    self.navBarLogo = imageView;

    [self.navigationController.navigationBar addSubview:imageView];

}

- (void)viewWillDisappear:(BOOL)animated {

    [self.navigationController.navigationBar sendSubviewToBack:self.navBarLogo];
}
Nick N
  • 984
  • 9
  • 22