Discussion Forums  >  Status Bar, Navigation Bar

Replies: 6    Views: 877

zed
Lost but trying
Profile
Posts: 2
Reg: Mar 07, 2014
Sydney
520
04/15/14 08:21 AM (5 years ago)

Customized Right Button on Navbar

Hello,

I am trying to have a custom right button which will display an image followed by some green text. Simple example would be

"Arrow Image" Next (in green text)

Using the following code I can get the "green text"

UIBarButtonItem *myRightButton = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"%d", _value] style:UIBarButtonItemStylePlain target:self action:nil];

[self.navigationItem setRightBarButtonItem:myRightButton];

self.navigationItem.rightBarButtonItem.tintColor = [UIColor greenColor];

How can I get an image before the "green text" also where will I place the image e.g. BT_images? Is this even do-able?

Many thanks.
Z
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
04/15/14 04:46 PM (5 years ago)
Hey zed, welcome. Yes it's totally doable, you just need to 'repeat' what you've done there but define an image instead of your text and then string them together in whatever order you want the buttons to appear.

eg if you want a system icon before or after your custom text button, plonk this in before or after (with whatever system icon you want):

UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(openTrustView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];

If you're looking at using a custom image, then you'll need to add the image to Xcode manually by dragging it in as you would a new plugin. Makes sense to keep a folder of your custom images within your project just for good housekeeping. Then ref that image file like this with your own parameters:

//Set the right nav button to a custom icon:

UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"special.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector (doSomethingCool)];

NSArray *actionButtonItems = @[myButton];
self.navigationItem.RightBarButtonItems = actionButtonItems;

That should work.

Cheers
Paddy.
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
04/15/14 04:50 PM (5 years ago)
... or have I misunderstood - do you want icon+text as one button? If so take a look at this: http://www.appcoda.com/customize-navigation-status-bar-ios-7/ there's some good stuff in there.

Cheers
Paddy
 
Niraj Shah
buzztouch Evangelist
Profile
Posts: 2937
Reg: Jul 11, 2012
Cerritos
37,870
like
04/15/14 08:28 PM (5 years ago)
Lookie there, we see Paddy dispensing advice as good as the best of them! :-)
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
04/15/14 08:40 PM (5 years ago)
Hehe, trying in vain to insert a mere sliver back into the karma wheel Niraj! :)
 
Tyroner
Aspiring developer
Profile
Posts: 175
Reg: Dec 26, 2012
Johannesburg, S...
4,200
like
04/16/14 02:56 AM (5 years ago)
Hi Paddy

Thanks for the code! Please advise where in Xcode I must insert the code.

Tyrone
 
zed
Lost but trying
Profile
Posts: 2
Reg: Mar 07, 2014
Sydney
520
like
04/16/14 03:49 AM (5 years ago)
Hey Paddy, works like a charm. Many thanks mate.

@Tyrone I am using it to customize the navigation bar of a plugin. In my case i decided to keep it in my 'plugin.m' file.

Cheers
Z
 

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.