2012-08-22 33 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

相關問題