0

I have a detailview with a navigation bar with a back button and a name for the view. The navigation bar is set programmatically. The name presented is set like this.

self.title = NSLocalizedString(name, @"");

The name depends on the presented view.

Now I would like to also present a small icon on the navigation bar which also is depends on the view.

How do I do that?

Pratyusha Terli
  • 2,343
  • 19
  • 31
Lars -
  • 501
  • 1
  • 10
  • 27
  • 1
    Solved it. The code placed under viewDidLoad UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStyleBordered target:self action:@selector(action:)]; self.navigationItem.rightBarButtonItem = addButton; [addButton release]; – Lars - Nov 21 '12 at 17:28
  • Yes! I found another way of doing it. Sorry none of the answers did work for me. Now I have a rightBar button with pictures depending of the view. – Lars - Nov 22 '12 at 15:05
  • o man previously i did not understand that you want navigation bar button...sorry for that, and sorry for late adding solution according to your query ..:) – Rajneesh071 Nov 22 '12 at 15:27
  • Didn´t have to be a button, just a picture in the right end of the bar, but a button did the trick for me. – Lars - Nov 23 '12 at 08:21
  • yup..in navigation bar you can set button and display image on it..:) – Rajneesh071 Nov 23 '12 at 08:23
  • But you can set picture also...no need to set button..just chk my answer again...:) – Rajneesh071 Nov 23 '12 at 08:30

4 Answers4

4

You can set backGround image to navigationBar Using this

Put in didFinishLaunchingWithOptions

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"title_bar.png"] forBarMetrics:UIBarMetricsDefault];  

Or you can set navigation bar image in any view using

UINavigationBar *navBar = [[self navigationController] navigationBar];
    UIImage *image = [UIImage imageNamed:@"TopBar.png"];
    [navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

Or you can set a view on your NavigationBar Link

[[UINavigationBar appearance] addSubview:yourView]; 

or

self.navigationItem.titleView = YourView;  

And set title using h

self.navigationItem.title = @"Your Title"; 

And you can get navigationBarButton using this

-(void)getRightBarBtn
{
    UIButton *Btn =[UIButton buttonWithType:UIButtonTypeCustom];

    [Btn setFrame:CGRectMake(0.0f,0.0f,68.0f,30.0f)];
    [Btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"yourImage.png"]]  forState:UIControlStateNormal];
    //[Btn setTitle:@"OK" forState:UIControlStateNormal];
    //Btn.titleLabel.font = [UIFont fontWithName:@"Georgia" size:14];
    [Btn addTarget:self action:@selector(yourBtnPress:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithCustomView:Btn];
    [self.navigationItem setRightBarButtonItem:addButton];
}

And set simple imageView on navigation Bar

UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"star.png"]];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:image];
self.navigationItem.rightBarButtonItem = backBarButton;
Community
  • 1
  • 1
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
  • self.title or self.navigationItem.title gives the same result and the lines for the image does not work. The bar does not show any picture – Lars - Nov 21 '12 at 09:07
  • Yes. It did alter the navbar to show icons in a row along the bar but it did only work when using iPhone, not using iPad. But unfortunately it does not suit the case I am working with but nice to know anyhow – Lars - Nov 21 '12 at 09:23
0

Add this code into viewDidLoad in your ViewController:

UIImage *image = [UIImage imageNamed: @"myIcon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(5, 2, 100, 39);
[self.navigationController.navigationBar addSubview:imageView];

imageView can also be declared as an instance variable, so you can access it after you've added it (e.g. if you want to remove your icon from your navigationBar at some point).

Oleg
  • 2,984
  • 8
  • 43
  • 71
  • If I add imageView as a property it still says Incompatible pointer type assigned to UIImage from UIImageView and regarding the frame it is not found on object of type UIImage – Lars - Nov 21 '12 at 09:12
  • Yes! I found another way of doing it. Sorry none of the answers did work for me. Now I have a rightBar button with pictures depending of the view. – Lars - Nov 22 '12 at 15:06
0

You need to subclass UINavigationBar. Then in drawRect do something like:

[[UIImage imageNamed...] drawInRect:...]

and call the super method ofcourse

Dimitar Marinov
  • 922
  • 6
  • 14
0
UIButton *homeBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 46, 28)];
[homeBtn setBackgroundImage:[UIImage imageNamed:@"homeBtn.png"] forState:UIControlStateNormal];    
[homeBtn addTarget:self action:@selector(homeBtnPressed) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *navHomeBtn=[[[UIBarButtonItem alloc] initWithCustomView:homeBtn] autorelease];    
self.navigationItem.rightBarButtonItem=navHomeBtn;   
[homeBtn release];
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Sumit Sharma
  • 1,847
  • 1
  • 22
  • 25