【问题标题】:How to change the selection area of a UIBarButtonItem?如何更改 UIBarButtonItem 的选择区域?
【发布时间】:2016-09-01 21:11:54
【问题描述】:

我正在尝试更改导航控制器中UIBarButtonItem 的选择区域。基本上我正在尝试更改允许UIBarButtonItem 可选的区域。现在,我的UIBarButtonItem 的大小是 40x40,但即使我的手指根本没有触摸按钮,我也可以轻松选择它。

这里是为了说明我的意思:

绿色代表UIBarButtonItem 的大小。红色代表使UIBarButtonItem 可选的允许区域。

如何更改红色区域的宽度?

如果有帮助,这里有一段代码:

changeMuscleMap = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40) )
changeMuscleMap.setImage(UIImage(named: "change"), forState: .Normal)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: changeMuscleMap)

谢谢!

【问题讨论】:

    标签: ios swift uinavigationcontroller uibarbuttonitem


    【解决方案1】:

    如果您想要比所需更大的点击区域,请尝试以下代码,然后使用自定义按钮制作 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
    

    【讨论】:

      【解决方案2】:

      您可以使用以下代码来增加或减少 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)
      

      【讨论】:

        【解决方案3】:

        可以通过将自定义项目设置为UIImageView 而不是UIButton 来实现UIBarButtonItem。试试下面的代码。我希望现在可以解决它。

        changeMuscleMap = UIImageView(image:UIImage(named: "change"))
        imageView.frame = CGRectMake(0, 0, 40, 40)
        let leftBarBtn = UIBarButtonItem(customView: imageView)
        self.navigationItem.leftBarButtonItem = leftBarBtn
        

        【讨论】:

        • 您建议的是解决方法,而不是解决方案,无论如何这与我的问题无关
        • 很抱歉误解了您的问题。我现在已经编辑了我的答案来解决这个问题。我希望这会有所帮助。
        【解决方案4】:

        通过使用@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)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-23
          • 2011-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多