如何更改 UIBarButtonItem 的选择区域?
How to change the selection area of a UIBarButtonItem?
我正在尝试更改导航控制器中 UIBarButtonItem
的 selection 区域。基本上,我正在尝试更改使 UIBarButtonItem
select 可用的允许区域。现在,我的 UIBarButtonItem
大小是 40x40,但我可以轻松地 select 即使我的手指根本没有碰到按钮。
这里是为了说明我的意思:
绿色代表UIBarButtonItem
的大小。红色代表使 UIBarButtonItem
select 可用的允许区域。
如何更改红色区域的宽度?
如果有帮助,这里有一段代码:
changeMuscleMap = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40) )
changeMuscleMap.setImage(UIImage(named: "change"), forState: .Normal)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: changeMuscleMap)
谢谢!
可以通过制作 UIBarButtonItem
并将自定义项目设为 UIImageView
而不是 UIButton
来实现。试试下面的代码。我希望现在能解决这个问题。
changeMuscleMap = UIImageView(image:UIImage(named: "change"))
imageView.frame = CGRectMake(0, 0, 40, 40)
let leftBarBtn = UIBarButtonItem(customView: imageView)
self.navigationItem.leftBarButtonItem = leftBarBtn
我能够通过使用@Damien Romito 在此处提供的答案实现我想要的:
UINavigationBar UIBarButtonItems much larger click area than required
已针对 Swift 3.0 更新:
let buttonContainer: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 27, height: 30) )
let barButton: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30) )
buttonContainer.addSubview(barButton)
barButton = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
barButton(#imageLiteral(resourceName: "image"), for: UIControlState.normal)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: buttonContainer)
barButton(self, action: #selector(ViewController.doSomething), for: UIControlEvents.touchUpInside)
您可以使用以下代码来增加或减少 UIBarButtonItem 按钮的宽度。
changeMuscleMap = UIButton(frame: CGRect(x: 0, y: 0, width: 120, height: 40) )
changeMuscleMap.setImage(UIImage(named: "change"), forState: .Normal)
changeMuscleMap.contentHorizontalAlignment = .left
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: changeMuscleMap)
如果你想要比要求更大的点击区域,试试下面的代码,然后制作一个带有自定义按钮的 UIBarButtonItem:
@implementation CustomButton
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
UIEdgeInsets extendTouchInsets = UIEdgeInsetsMake(20, 0, 20, 0);
CGRect bounds = self.bounds;
bounds.origin.x -= extendTouchInsets.left;
bounds.origin.y -= extendTouchInsets.top;
bounds.size.width += extendTouchInsets.left + extendTouchInsets.right;
bounds.size.height += extendTouchInsets.top + extendTouchInsets.bottom;
return CGRectContainsPoint(bounds, point);
}
@end
我正在尝试更改导航控制器中 UIBarButtonItem
的 selection 区域。基本上,我正在尝试更改使 UIBarButtonItem
select 可用的允许区域。现在,我的 UIBarButtonItem
大小是 40x40,但我可以轻松地 select 即使我的手指根本没有碰到按钮。
这里是为了说明我的意思:
绿色代表UIBarButtonItem
的大小。红色代表使 UIBarButtonItem
select 可用的允许区域。
如何更改红色区域的宽度?
如果有帮助,这里有一段代码:
changeMuscleMap = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40) )
changeMuscleMap.setImage(UIImage(named: "change"), forState: .Normal)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: changeMuscleMap)
谢谢!
可以通过制作 UIBarButtonItem
并将自定义项目设为 UIImageView
而不是 UIButton
来实现。试试下面的代码。我希望现在能解决这个问题。
changeMuscleMap = UIImageView(image:UIImage(named: "change"))
imageView.frame = CGRectMake(0, 0, 40, 40)
let leftBarBtn = UIBarButtonItem(customView: imageView)
self.navigationItem.leftBarButtonItem = leftBarBtn
我能够通过使用@Damien Romito 在此处提供的答案实现我想要的:
UINavigationBar UIBarButtonItems much larger click area than required
已针对 Swift 3.0 更新:
let buttonContainer: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 27, height: 30) )
let barButton: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30) )
buttonContainer.addSubview(barButton)
barButton = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
barButton(#imageLiteral(resourceName: "image"), for: UIControlState.normal)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: buttonContainer)
barButton(self, action: #selector(ViewController.doSomething), for: UIControlEvents.touchUpInside)
您可以使用以下代码来增加或减少 UIBarButtonItem 按钮的宽度。
changeMuscleMap = UIButton(frame: CGRect(x: 0, y: 0, width: 120, height: 40) )
changeMuscleMap.setImage(UIImage(named: "change"), forState: .Normal)
changeMuscleMap.contentHorizontalAlignment = .left
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: changeMuscleMap)
如果你想要比要求更大的点击区域,试试下面的代码,然后制作一个带有自定义按钮的 UIBarButtonItem:
@implementation CustomButton
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
UIEdgeInsets extendTouchInsets = UIEdgeInsetsMake(20, 0, 20, 0);
CGRect bounds = self.bounds;
bounds.origin.x -= extendTouchInsets.left;
bounds.origin.y -= extendTouchInsets.top;
bounds.size.width += extendTouchInsets.left + extendTouchInsets.right;
bounds.size.height += extendTouchInsets.top + extendTouchInsets.bottom;
return CGRectContainsPoint(bounds, point);
}
@end