如何向UINavigationBar添加按钮

时间:2012-08-22 14:58:38

标签: ios uinavigationcontroller uibarbuttonitem

我需要这样的东西 http://anishusite.appspot.com/hbimgs/Bills220.png

能够从导航栏更改日期......这是怎么做到的? 我尝试添加一个带有thous按钮的工具栏和一个带有日期标签的视图但是效果不好

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

以下是代码:

  //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

你可以看到它看起来不太好。标签没有显示......按钮似乎只有在上半部分轻敲时才有效。 (当点击方法被称为ok)...任何想法? BTW导航栏有一个背景图片,但是工具栏覆盖了它..为什么?我制作了一个带有清晰颜色背景的工具栏....

1 个答案:

答案 0 :(得分:2)

UINavigationItem有一个titleView属性。您可以将其设置为您创建的任何视图。因此,创建一个透明背景的UIToolbar子类,添加按钮和文本标签,然后:

self.navigationItem.titleView = myCustomView;

其中myCustomView是包含箭头按钮和标签的视图。这个视图可以是一个UIToolbar子类,读取Couldn't UIToolBar be transparent?有关创建透明的UIToolbar子类的技巧,然后添加按钮:

flex spacer,左箭头,带有堆叠UILabels的自定义视图,右箭头,flex spacer