UIBarButtonItem Connection Scope

I create a left button element with the following 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 to add a button on the View, because when I use CGRectOffset for the button, it does not work. So, in this case, my button on one of the UIViewController showed

enter image description here

So this is what I want. But the UIBarButton only started clicking on the UIBarButton (rectangle area). But when I add only the button to the UIBarButtonItem, it works even if I click on the UINavigationBar area that you can see on the screen.

I add a 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 is shown as follows

enter image description here

So, CGRectOffset does not work, and my UIBarButton is very close to the left side of the screen, but it works fine by touching the entire area on the navigation bar that you see on the screen. Therefore, I can touch all areas in the navigation bar, and UIBarButton will work fine

I need: UIBarButton should be displayed as in the first case with a larger area on the left side. But it should work by pressing not only the button, but also all the surrounding sounds, as in the second case. What should I do?

EDIT In the first case, the button is pressed if I touch the next area

enter image description here

In the second case, the button is pressed if I touch the next area

enter image description here

I need the button position, as in the first case (more origin.x on the left side), but the clickable area, as in the second case

+6
source share
4 answers

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

You can override this method in your own subclass to increase or decrease the actual scope of the view (or the button in your case). For example, to increase the hit area 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 intended to reduce CGRect, therefore, by specifying a negative value, the function will expand CGRect.

Hope this helps!

+3
source

A view frame is the size and position of a view in supervision (its parent). So when you go to set the backBtnView initWithFrame , try:

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

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

0
source

You can use UIEdgeInsetsMake. This works 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]; 
0
source

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

0
source

Source: https://habr.com/ru/post/1483797/


All Articles