导航栏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系统图标的按钮)

转载于:https://www.cnblogs.com/knightguang/p/9605738.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值