2012-08-22 32 views
2

我需要的东西就是这样的 http://anishusite.appspot.com/hbimgs/Bills220.png如何按钮添加到UINavigationBar的

能够改变从导航栏的日期......这怎么办呢? 我尝试添加与古都按钮的工具栏,并与日期标签的视图,但它不工作得很好

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)......任何ideeas? BTW导航栏有一个背景图像,但是在工具栏覆盖它。为什么?我有一个清晰的彩色背景制作工具栏....

+0

什么不工作?您是否添加了方法来在您点击按钮后更改日期? – Josh

回答

2

UINavigationItem有titleview的财产。您可以将其设置为您创建的任何视图。因此,创建一个UIToolbar子与透明背景,添加您的按钮和文本标签,这一点,那么:

self.navigationItem.titleView = myCustomView; 

其中myCustomView是包含您的箭头按钮和标签的视图。这种观点可能是一个UIToolbar子类,创建一个UIToolbar子类,它是透明的,然后加上你的按钮阅读Couldn't UIToolBar be transparent?的招数:

柔性垫片,左箭头,用叠UILabels,右箭头自定义视图,柔性垫片

+0

不工作也很好...按钮似乎无响应,看起来可怕...会张贴代码... – user1028028

相关问题