ナビゲーションバーNavigationBarのボタン設定

1626 ワード

navigationBarの左右ボタンをカスタマイズするとbuttonの画像が大きく見える場合がありますが、個人的な感覚の原因は以下のような場合があります.
1、UIButtonを使用してnavigationBarに直接追加する2、UIViewをカスタマイズし、システムのNavigationBarに代わってbuttonのframe値設定に問題がある.
1つ目のケースの解決方法は次のとおりです.
    // 

   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;

第2のケースについては、システムのボタンを取得するだけである、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 = (navigation barの右側にあるrefreshシステムアイコンを使用したボタンです)