Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 4    Views: 61

NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
03/22/14 06:58 PM (10 years ago)

Adding icon with code (instead of text) to NavBar

I have added the following code which works. How would I add icon image instead of the text? // this approach uses text, but would like to include a button image UIBarButtonItem *myRightButton = [[UIBarButtonItem alloc] initWithTitle:@"Do It" style:UIBarButtonItemStylePlain target:self action:@selector(doStuff)]; [self.navigationItem setRightBarButtonItem:myRightButton]; } -(void) doStuff{ // will add the rest of my code here }
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
03/23/14 05:02 AM (10 years ago)
Hi NCbuzz, I'm compiling a list of ways to mess around with nav bar buttons... see if any of these do what you want. You'll need to change the settings to your right button (these are for back buttons atm) but there are a couple of image options amongst them that might get you close: //Need these next three lines for all options - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // self.title = @"Leave this blank for the default SCREEN title for the second screen in a nav stack, or change it if you need to"; // self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"list.png"] style:(UIBarButtonItemStylePlain) target:nil action:nil]; //This simply replaces the "Back" chevron (<) with an icon of your choosing. self.navigationController.navigationBar.backIndicatorImage = [UIImage imageNamed:@"pin.png"]; // Setting this to blank.png gives a different result... self.navigationController.navigationBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"blank.png"]; //This overrides the default label and gives an empty label "" instead self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]; //Don’t forget to close the brace... } Cheers Paddy
 
NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
like
03/23/14 09:08 AM (10 years ago)
Away from my computer until later today... But I think that will do it :) Replacing my initWithText with the initWithImage should work. Thanks Paddy!!
 
NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
like
03/23/14 07:07 PM (10 years ago)
Worked as advertised. Thanks again :)
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
03/23/14 07:17 PM (10 years ago)
No problems! Feels weird to have the advice flowing FROM me for a change! ;)
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.