twitter rss

How can I put two buttons at the same in a navigation bar?

Labels:
yep,

Here is the problem:
I want to put these 2 buttons at the same site(right). The standard navigation bar in iphone doesnt allow me to do this, I mean, there is just one placeholder for the right button. So the solution which I took was doing a sort of hack component to fit 2 buttons, here is the code:



-(UIBarButtonItem *)makeRightBarButtonItem {
// create a toolbar to have two buttons in the right
TransparentToolbar* tools = [[TransparentToolbar alloc] initWithFrame:CGRectMake(0, 0, 90, 44.01)];
tools.tintColor = [UIColor clearColor];


// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

// create a calendar button
UIBarButtonItem* toolBarItem = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:@"calendarIcon.png"]
style:UIBarButtonItemStyleBordered
target:self
action:@selector(callCalendar:)
];

[buttons addObject:toolBarItem];
[toolBarItem release];

// create a spacer
toolBarItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:toolBarItem];
[toolBarItem release];

// create a standard "add" button
toolBarItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(callJournalEntry:)];
toolBarItem.style = UIBarButtonItemStyleBordered;
[buttons addObject:toolBarItem];
[toolBarItem release];

// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];

[buttons release];

//the toolbar is our right bar button
UIBarButtonItem *theButton = [[[UIBarButtonItem alloc] initWithCustomView:tools] autorelease];
[tools release];

return theButton;
}
However, as you can see I`m not using a normal toolbar, I`m using a "TransparentToolbar" which I had to create as well, I just had to do it to make my navigation bar black, so here is the code for the toolbar:

TransparentToolbar.h


#import <Foundation/Foundation.h>


@interface TransparentToolbar : UIToolbar
@end

TransparentToolbar.m

#import "TransparentToolbar.h"


@implementation TransparentToolbar

// Override draw rect to avoid
// background coloring
- (void)drawRect:(CGRect)rect {
    // do nothing in here
}

// Set properties to make background
// translucent.
- (void) applyTranslucentBackground
{
    self.backgroundColor = [UIColor clearColor];
    self.opaque = NO;
    self.translucent = YES;
}

// Override init.
- (id) init
{
    self = [super init];
    [self applyTranslucentBackground];
    return self;
}

// Override initWithFrame.
- (id) initWithFrame:(CGRect) frame
{
    self = [super initWithFrame:frame];
    [self applyTranslucentBackground];
    return self;
}

@end
That`s it, now you have your bar with two buttons at the right site.

Post a Comment

Followers

Text Widget