Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

enter image description here

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

enter image description here

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

enter image description here

In second case button clicked if i tap next area

enter image description here

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

like image 975
user2213271 Avatar asked May 31 '13 13:05

user2213271


2 Answers

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];
like image 40
JAY THUMMAR Avatar answered Oct 16 '22 16:10

JAY THUMMAR


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!

like image 168
Tom Hancocks Avatar answered Oct 16 '22 14:10

Tom Hancocks