简体   繁体   中英

UIBarButtonItem tap area

I create left Bar button item with next method:

- (void)leftButtonOnNavigationItem:(NSString *)imageName type:(NSInteger)type
{
    UIImage *backImg = [UIImage imageNamed:imageName];
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0.f, 0.f, backImg.size.width, backImg.size.height)];
    if (type == 0)
        [backBtn addTarget:self.navigationController action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    else
        [backBtn addTarget:self action:@selector(leftMake) forControlEvents:UIControlEventTouchUpInside];
    backBtn.contentMode = UIViewContentModeCenter;
    [backBtn setImage:backImg forState:UIControlStateNormal];
    UIView *backBtnView = [[UIView alloc] initWithFrame:backBtn.bounds];
    backBtnView.bounds = CGRectOffset(backBtnView.bounds, -6, 0);
    [backBtnView addSubview:backBtn];
    UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc] initWithCustomView:backBtnView];
    self.navigationItem.leftBarButtonItem = backBarBtn;
}

I need add button on View because when I use CGRectOffset for button it is not work. So with this case my button on one od the UIViewController shown

在此输入图像描述

So it is what I want. But UIBarButton began clicked only on UIBarButton (area of the rectangle). But when I add only button in UIBarButtonItem it is work even if I clicked on area of UINavigationBar that you can see on screen.

I add button like this:

- (void)leftButtonOnNavigationItem:(NSString *)imageName type:(NSInteger)type
{
    UIImage *backImg = [UIImage imageNamed:imageName];
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0.f, 0.f, backImg.size.width, backImg.size.height)];
    if (type == 0)
        [backBtn addTarget:self.navigationController action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    else
        [backBtn addTarget:self action:@selector(leftMake) forControlEvents:UIControlEventTouchUpInside];
    backBtn.contentMode = UIViewContentModeCenter;
    [backBtn setImage:backImg forState:UIControlStateNormal];
//    UIView *backBtnView = [[UIView alloc] initWithFrame:backBtn.bounds];
//    backBtnView.bounds = CGRectOffset(backBtnView.bounds, -6, 0);
//    [backBtnView addSubview:backBtn];
    UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc] initWithCustomView:backBtn];//View];
    self.navigationItem.leftBarButtonItem = backBarBtn;
}

and now my button shown like this

在此输入图像描述

So CGRectOffset not work and my UIBarButton very close to left side of screen, but it is work fine by tap all area on navigation bar, that you see on screen. So I can touch in all area on navigation Bar and UIBarButton will work fine

So I need: UIBarButton must shown like in first case with more area from left side. But it must work by click not only button but all surround ares like in second case. What I must do?

EDIT In first case button clicked if i tap next area

在此输入图像描述

In second case button clicked if i tap next area

在此输入图像描述

I need position of button like in first case (more origin.x from left side) but clickable area like in second case

All controls/elements that can be interacted with inherit from UIView. UIView provides a method called - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;

It is possible to reimplement this method in your own subclass to increase or decrease the valid hit region of the view (or button in your case). For example, to grow the hit region by 5 points in all directions

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    return CGRectContainsPoint( CGRectInset( self.bounds, -5, -5 ), point );
}

Note the negative values when using CGRectInset(). This is because the function is designed for shrinking a CGRect, so by specifying a negative value, the function will expand the CGRect.

Hope this helps!

A view's frame is the size and position of the view within a superview (its parent). So when you go to set backBtnView initWithFrame try:

UIView *backBtnView = [[UIView alloc] 
                       initWithFrame:CGRectOffset(backBtn.frame, -6.0, 0.0)];

and forget about bounds all together, I think that will put you in the right direction.

You can use UIEdgeInsetsMake. It work for me.

UIImage *buttnImage=[UIImage imageNamed:imageName];
UIButton *backButton=[UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:buttnImage forState:UIControlStateNormal];
backButton.frame=CGRectMake(0, 0, (buttnImage.size.width+leftMargin+rightMargin), (buttnImage.size.height+topMargin+bottomMargin));
[backButton setImageEdgeInsets:UIEdgeInsetsMake(topMargin, leftMargin, bottomMargin, rightMargin)];

UIBarButtonItem *backBtn=[[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.leftBarButtonItems=@[backBtn];

I fixed it in used accessibility frame. Just set accessibility frame for button with increased height and width and it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM