2

I need something just like this http://anishusite.appspot.com/hbimgs/Bills220.png

to be able to change the date from the navigation bar ... How is this done? I tried adding a toolbar with thous buttons and a view with the date label but it's not working very well

http://osmorphis.blogspot.ro/2009/05/multiple-buttons-on-navigation-bar.html

Here is the code:

  //first create the view 
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 6, 157, 33)];
[titleView setBackgroundColor:[UIColor clearColor]];

//add the buttons to the view
UIButton *prevButton = [UIButton buttonWithType:UIButtonTypeCustom];
[prevButton setFrame:CGRectMake(0, 1, 32, 32)];
[prevButton setBackgroundImage:[UIImage imageNamed:@"36-circle-west"] forState:UIControlStateNormal];
[prevButton addTarget:self action:@selector(prevMonth) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:prevButton];


UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
[nextButton setFrame:CGRectMake(145, 1, 32, 32)];
[nextButton setBackgroundImage:[UIImage imageNamed:@"20-circle-east"] forState:UIControlStateNormal];
[nextButton addTarget:self action:@selector(nextMonth) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:nextButton];

[self.dateLabel setFrame:CGRectMake(33, 1, 90, 33)];
[self.dateLabel setBackgroundColor:[UIColor clearColor]];
[self.dateLabel setTextAlignment:UITextAlignmentCenter];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:@"MMMM,YYYY"];
NSString *dateString = [dateFormatter stringFromDate:self.beginDate];
NSLog(@"the title date string: %@",dateString);

[self.dateLabel setText:dateString];
[self.dateLabel setTextColor:[UIColor whiteColor]];
[self.dateLabel setBackgroundColor:[UIColor clearColor]];
[self.dateLabel setFont:[UIFont fontWithName:@"System" size:17.0]];

//create the toolbar itself

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 240, 44)];
[toolbar setBackgroundColor:[UIColor clearColor]];

NSMutableArray *items  = [NSMutableArray arrayWithCapacity:3];

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addTransaction)];
addButton.style = UIBarButtonItemStyleBordered;


UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIBarButtonItem *theView = [[UIBarButtonItem alloc] initWithCustomView:titleView];

[items addObject:theView];
[items addObject:spacer];
[items addObject:addButton];

[toolbar setItems:items animated:NO];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];

http://www.shareimages.com/image.php?61601-qpyWmpSfk5.kmpyWmak-screen.png

As you can see it doesn't look good. The Label is not showing... The buttons seem to work only if tapped on the upper half. (when tapped the methods are called ok ) ... any ideeas? BTW the navigation bar has a background image, however the toolbar covers it up.. why ? I made the toolbar with a clear color background ....

user1028028
  • 6,323
  • 9
  • 34
  • 59
  • What's not working? Have you added methods to change the date once you click the button? – Josh Aug 22 '12 at 15:00

1 Answers1

2

UINavigationItem has a titleView property. you can set this to any View you create. So create a UIToolbar subclass with transparent background, add your buttons and text labels to that, then:

self.navigationItem.titleView = myCustomView;

where myCustomView is the view containing your arrow buttons and labels. This view could be a UIToolbar subclass, read Couldn't UIToolBar be transparent? for tricks on creating a UIToolbar subclass that is transparent, then add your buttons:

flex spacer, left arrow, custom view with stacked UILabels, right arrow, flex spacer

Community
  • 1
  • 1
CSmith
  • 13,318
  • 3
  • 39
  • 42