iOS開發導航欄控件的作用

一,在iOS開發過程中針對一些導航欄上需要自定義視圖的情況,有時候需要用系統自帶的處理,有些時候需要自定義一些視圖並把視圖添加上去,這時候主要是它們的位置有些許差別,下面簡單寫下demo;

1,用導航欄系統自帶的視圖處理:

 1     //1 中間的圖片
 2     UIImageView *imageBarView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth / 2.f - 40.f, 20.f, 80, 30)];
 3     imageBarView.image = [UIImage imageNamed:@"中間視圖名稱"];
 4     self.navigationItem.titleView = imageBarView;
 5     
 6     
 7     //2 導航欄右邊的分類按鈕
 8     UIButton *buttonRightItem = [UIButton buttonWithType:UIButtonTypeCustom];
 9     buttonRightItem.frame = CGRectMake(0.f, 0.f, 50.f, 30.f);
10     [buttonRightItem setTitle:@"分類" forState:UIControlStateNormal];
11     [buttonRightItem setTitleColor:[UIColor colorWithRed:0.310 green:0.598 blue:0.156 alpha:1.000] forState:UIControlStateNormal];
12     buttonRightItem.titleLabel.font = [UIFont systemFontOfSize:17];
13     [buttonRightItem addTarget:self action:@selector(buttonRightAction:) forControlEvents:UIControlEventTouchUpInside];
14     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonRightItem];
15     
16     //3 導航欄左側的APP按鈕
17     UIButton *buttonLeftItem = [UIButton buttonWithType:UIButtonTypeCustom];
18     buttonLeftItem.frame = CGRectMake(0.f, 0.f, 64.f, 64.f);
19     [buttonLeftItem setImage:[UIImage imageNamed:@"左側視圖"] forState:UIControlStateNormal];
20     buttonLeftItem.imageEdgeInsets = UIEdgeInsetsMake(0.f, -30.f, 0.f, 0.f);//用來調節相對位置
21     [buttonLeftItem addTarget:self action:@selector(buttonLeftAction:) forControlEvents:UIControlEventTouchUpInside];
22     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonLeftItem];

其實以上關鍵是:

self.navigationItem.titleView = imageBarView;

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonRightItem];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonLeftItem];

主要是上面的三句話,這個優點是左,中,右三個按鈕的響應事件的區域比較大,比較容易調控,一般建議用系統自帶的來操作;

缺點也有,就是每個區域只能放一個控件,下面用自定義的方法來在同一個區域放兩個或以上的控件;

2,自定義導航欄上的視圖;

 1      rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
 2     rightButton.backgroundColor = [UIColor blackColor];
 3     [rightButton setTitle:@"分享" forState:UIControlStateNormal];
 4     [rightButton addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
 5     rightButton.frame = CGRectMake(self.view.width - 50.f,0.f, 50.f, 44.f);
 6     [self.navigationController.navigationBar addSubview:rightButton];
 7     
 8     rightTwoButton = [UIButton buttonWithType:UIButtonTypeCustom];
 9     rightTwoButton.backgroundColor = [UIColor blueColor];
10     [rightTwoButton setTitle:@"收藏" forState:UIControlStateNormal];
11     [rightTwoButton addTarget:self action:@selector(storeAction:) forControlEvents:UIControlEventTouchUpInside];
12     rightTwoButton.frame = CGRectMake(self.view.width - 100.f, 0.f, 50.f, 44.f);
13     [self.navigationController.navigationBar addSubview:rightTwoButton];

 

以上方法可以把視圖很任性的放在導航欄的任何位置,因爲用的是 self.navigationController.navigationBar 來添加的,這個是代表整個導航欄,但是這個的frame需要認真調節,不然的話佈局會很難看,除此之外,噹噹起視圖要出現或者消失時還需要對這個按鈕進行處理;

 1 - (void)viewWillDisappear:(BOOL)animated
 2 {
 3     [super viewWillDisappear:animated];
 4     
 5     [UIView animateWithDuration:0.2 animations:^{
 6         
 7         rightButton.alpha = 0;
 8         rightTwoButton.alpha = 0;
 9 
10     }];
11 }
12 
13 - (void)viewWillAppear:(BOOL)animated{
14     
15     [super viewWillAppear:animated];
16     [UIView animateWithDuration:0.2 animations:^{
17         
18         rightButton.alpha = 1;
19         rightTwoButton.alpha = 1;
20         
21     }];
22 
23 }

以上就是導航欄上視圖的處理,可能比較簡單,但是對初學者來說也是比較重要的;

小技巧:消除導航欄下方的陰影辦法:

// 添加上這一句,可以去掉導航條下邊的shadowImage,就可以正常顯示了  
 self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];  
 self.navigationController.navigationBar.translucent = NO; 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章