首页 文章

将leftbarbuttonitem的可点击区域限制为自定义uibarbuttonitem的大小

提问于
浏览
2

我在我的应用上实现了SWReveal控件 . 为了解决这个问题,我有一个带有自定义视图的UIBarButtonItem . 好吧,一切都工作正常,但我注意到,即使点击远离图像(大约44px宽的图像......并点击高达100px),我也可以发射事件 .

我一直在寻找,并找到了我找到的最佳解决方案(定义自定义按钮并使用“initWithCustomView”实例化UIBarButtonItem)

这是我之前的代码:

UIBarButtonItem *revealButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reveal-icon"] style:UIBarButtonItemStylePlain target:_revealController action:@selector(revealToggle:)];
self.navigationItem.leftBarButtonItem = revealButtonItem;

而代码与“解决方案”:

//create the image for your button, and set the frame for its size
UIImage *image = [UIImage imageNamed:@"reveal-icon"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
//init a normal UIButton using that image
UIButton* button = [[UIButton alloc] initWithFrame:frame];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:_revealController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
//finally, create your UIBarButtonItem using that button
UIBarButtonItem* revealButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

self.navigationItem.leftBarButtonItem = revealButtonItem;

有了这个后面的代码,我知道我有一个正确大小的按钮(我设置了backgroundColor,并检查了按钮的正确大小),但是当点击远离按钮时仍然触发动作 .

有任何想法吗?

先感谢您 .

4 回答

  • 0

    尝试使用 button.clipsToBounds = YES; ,但我必须同意其他评论,根据Apple的iOS Human Interface Guidelines,每个可点击区域应至少为44x44 .

  • 0

    自定义视图路径似乎对我有用,但我确实找到了另一种解决方案,如果这对您不起作用,您可以尝试 . 似乎可点击区域只会延伸到远远超过用户可以点击的东西 . 我有一个 Headers 视图,用户可以点击以执行某些操作,因此我的leftbarbuttonitem的可点击区域停在那里 . 但我也尝试添加另一个uiview,userinteractionenabled设置为yes只是略微在leftbarbuttonitem右侧,这似乎阻止该区域扩展 . 我只能使用两行标准代码而不是自定义代码 . 不确定这是否适合你

  • 0
    It is hard to make uibarbuttonitem touch area override. We can achieve the same using subclassing uinavigationbar.
    
    File-New-CocoaTouch Class-UINavigationBar- Name it.
    
    This is mine:
    
    import UIKit
    import Foundation
    
    let navigationItem : UINavigationItem = UINavigationItem()
    
    class CustomNavigationBar: UINavigationBar {
    
        override init(frame: CGRect) {
            super.init(frame: frame)
    
            self.backgroundColor = UIColor.redColor()
        }
        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)!
        }
        override func drawRect(rect: CGRect) {
    
        }
        func addUIButtonToUIBarButtonitems(leftButton:UIButton,leftButtonFrame:CGRect,rightButton:UIButton,rightButtonFrame:CGRect)
        {
            let leftView = UIView(frame: leftButtonFrame)
            leftView.addSubview(leftButton)
    
            let leftItem:UIBarButtonItem = UIBarButtonItem(customView: leftView)
            navigationItem.leftBarButtonItem = leftItem
    
            let rightView = UIView(frame: rightButtonFrame)
            rightView.addSubview(rightButton)
    
            let rightItem:UIBarButtonItem = UIBarButtonItem(customView: rightView)
            navigationItem.rightBarButtonItem = rightItem
    
            items = [navigationItem]
        }
    }
    
    I have a method for setting uibuttons as uibarbuttonitems. On View Controller, I will add two unbuttons and will pass the same to the navigation bar. The frame of the unbutton will be the click area of the respective uibattonitem.
    
    class CustomNavBarVC: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.styleCustomNavigationBar()
        }
    
        func styleCustomNavigationBar() {
    
            self.navigationController?.setNavigationBarHidden(true, animated: false)
    
            let navigationBar : CustomNavigationBar = CustomNavigationBar(frame: CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64.0))
    
            //menu button
            let menubutton: UIButton = UIButton(frame: CGRectMake(0,0,30,30))
            menubutton.setImage(UIImage(named: "menu"), forState: UIControlState.Normal)
            menubutton.addTarget(self, action: "backButtonDidClicked", forControlEvents: UIControlEvents.TouchUpInside)
    
            //search button
            let searchbutton: UIButton = UIButton(frame: CGRectMake(0,0,30,30))
            searchbutton.setImage(UIImage(named: "search1x"), forState: UIControlState.Normal)
            searchbutton.addTarget(self, action: "backButtonDidClicked", forControlEvents: UIControlEvents.TouchUpInside)
    
    
            navigationBar.addUIButtonToUIBarButtonitems(menubutton, leftButtonFrame: CGRectMake(0,0,30,30), rightButton: searchbutton, rightButtonFrame: CGRectMake(0,0,30,30))
    
            self.view.addSubview(navigationBar)
        }
    
        func backButtonDidClicked()
        {
            print("CLICK ")
        }
    }
    
  • 0

    你可以试试这个 button.clipsToBounds = YES;

    这可能对你有所帮助 .

相关问题