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

时间:2015-01-26 19:19:57

标签: ios uibarbuttonitem

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

我一直在搜索,找到了我找到的最佳解决方案(定义自定义按钮并使用“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 :(得分:0)

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

答案 1 :(得分:0)

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

答案 2 :(得分: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 ")
    }
}

答案 3 :(得分:0)

您可以尝试此button.clipsToBounds = YES;

这可能会对你有帮助。