導航欄NavigationBar的按鈕設置


有時候在自定義navigationBar的左右按鈕的時候,button的圖片會顯得很大,個人感覺原因有以下幾種情況:

1、使用的是UIButton直接加在navigationBar上面
2、自定義了一個UIView,頂替系統的NavigationBar,button的frame值設置有問題。

如果是第一種情況解決方法如下:

    //左邊城市選擇
   UIButton *LButton =[UIButton buttonWithType:UIButtonTypeCustom];
   LButton.frame = CGRectMake(0, 0, 40, 30);
   LButton.imageEdgeInsets= UIEdgeInsetsMake(0, 20, 0, 0);
   [LButton setTitle:@"長沙" forState:UIControlStateNormal];
   LButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
   UIBarButtonItem *leftButton=[[UIBarButtonItem alloc]initWithCustomView:LButton];

   self.navigationItem.leftBarButtonItem = leftButton;
 
   // 右邊search按鈕
   UIButton *RButton =[UIButton buttonWithType:UIButtonTypeCustom];
   RButton.frame = CGRectMake(0, 0, 40, 30);
   [RButton setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
   
   UIBarButtonItem *rightButton=[[UIBarButtonItem alloc]initWithCustomView:RButton];  
   self.navigationItem.rightBarButtonItem = rightButton;

針對第二種情況:
如果只是想獲取系統的按鈕的話, 因為 navigation bar 上的按鈕是 UIBarButtonItem 類的, 所以位置和大小是按內容自動生成的, 獲取時也需要通過UIEvent 獲取. 例如:

- (void)myButtonPressed:(UIBarButtonItem *)button event:(UIEvent *)event 
{
   NSLog(@"%@", [event.allTouches.anyObject view]);
}

輸出: UINavigationButton: 0x71cf400; frame = (281 7; 34 30); opaque = NO; layer = <CALayer: 0x718e950>
 (這是一個位於navigation bar右側使用了refresh系統圖標的按鈕)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM