我们直接在leftBarItem添加一个自定义button的时候,这个button的有效点击范围将会很大,接近navigationBar的titleview。
如果我们只想要响应范围是按钮的区域的时候。可进行如下操作:
+(instancetype)barButtonItemIcon:(NSString *)icon HighlightedIcon:(NSString *)highIcon Target:(id)target action:(SEL)action{
UIButton *button = [[UIButton alloc] init];
[button setBackgroundImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:highIcon] forState:UIControlStateHighlighted];
button.frame = (CGRect){CGPointZero,button.currentBackgroundImage.size};
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
//先将你的按钮添加到一个同样大小的uiview中,然后再将这个uiview添加到leftBarItem里。
UIView *view = [[UIView alloc] initWithFrame:button.frame];
[view addSubview:button];
return [[UIBarButtonItem alloc]initWithCustomView:view];
}