2015-04-17 54 views
0

我在自定義UINavigationBar中掙扎。我想定製的原因是因爲在不同的視圖控制器中可能會發生相同的操作。作爲例子,我張貼了一些圖片。如何自定義UINavigationBar

Navigation Bar - 1 Navigation Bar - 2 Navigation Bar - 3

正如你可以從上面的圖片看。我想你明白我的意思。我一直在努力尋找,但我沒有得到我想要的答案。此外,我已經嘗試使用UIView來實現這一點,它的工作原理,但我不認爲這是做到這一點的正確方法。因此,我需要你們的幫助,讓我走向正確的道路。 :) :) 謝謝。

回答

0

您可以像這樣將自定義標題視圖設置爲導航欄。

//To hide default back button 
self.navigationItem.hidesBackButton=YES; 

//Title View for Navigation 
UIView *navTitle1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; 
[navTitle1 setBackgroundColor:[UIColor lightGrayColor]]; 

//Left View button and separator 
UIButton *backBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)]; 
[backBarButton setTitle:@"Back" forState:UIControlStateNormal]; 

UIView *ttlview1 = [[UIView alloc] initWithFrame:CGRectMake(51, 0, 1, 44)]; 
[ttlview1 setBackgroundColor:[UIColor darkGrayColor]]; 


//Center view with two buttons and seprator for them 
UIView *middleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 121, 44)]; 
[middleView setBackgroundColor:[UIColor clearColor]]; 
[middleView setCenter:navTitle1.center]; 

UIButton *postBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)]; 
[postBarButton setTitle:@"Post" forState:UIControlStateNormal]; 

UIView *ttlview2 = [[UIView alloc] initWithFrame:CGRectMake(middleView.frame.size.width/2, 0, 1, 44)]; 
[ttlview2 setBackgroundColor:[UIColor darkGrayColor]]; 

UIButton *memeBarButton = [[UIButton alloc] initWithFrame:CGRectMake((middleView.frame.size.width/2)+1, 0, 60, 44)]; 
[memeBarButton setTitle:@"Meme" forState:UIControlStateNormal]; 

[middleView addSubview:ttlview2]; 
[middleView addSubview:postBarButton]; 
[middleView addSubview:memeBarButton]; 

//Right button with seprator before that 
UIView *ttlview3 = [[UIView alloc]  initWithFrame:CGRectMake(self.view.frame.size.width-71, 0, 1, 44)]; 
[ttlview3 setBackgroundColor:[UIColor darkGrayColor]]; 

有關更多詳細信息,請參閱THIS

+0

這是的UIBarButtonItem我之前做了什麼。這是正確的,而不是自定義。 – tan

+0

我認爲,這是方式,你可以做高級別的定製,默認導航欄不提供通過本機功能和按鈕直接做這種定製。 –

0

您可以從2次創建2個自定義的UIBarButtonItem,使用本文件:

從蘋果文檔: 使用指定的自定義視圖的新項目。

- (id)initWithCustomView:(UIView *)customView 

參數customView:表示項目A自定義視圖。 返回值具有指定屬性的新初始化項目。

  • 左欄項目:1視圖包含3個按鈕:項目,Kiara Paradize,藝術家印象。

  • 右欄項目:1視圖包含4個按鈕:註冊|登錄(用戶登錄時的配置文件按鈕)|漢堡菜單

然後:

  • self.navigationItem.leftBarButtonItem =從左 自定義視圖
  • self.navigationItem.rightBarButtonItem =的UIBarButtonItem從右自定義視圖