React-native-navigation: Increasing Tappable Area of Top Bar Buttons

Created on 18 Apr 2019  路  12Comments  路  Source: wix/react-native-navigation

Issue Description

When using custom icons for left/right buttons, the tappable area is significantly reduced as compared to native iOS icons.

Is there any way to improve the tappable area of custom buttons to extend to the edge of the screen and more vertically?

Steps to Reproduce / Code Snippets / Screenshots

Example of the tappable area of native iOS icons created using :

rightButtons: [
  {
    id: "cart",
    systemItem: "add"
    color: "gray"
  }
]

rnn-system-icon

Versus the tappable area of custom icons created using:

rightButtons: [
  {
    id: "cart",
    icon: images.shopping_cart,
    color: "gray"
  }
]

rnn-png-icon

Environment

  • React Native Navigation version: 2.17.0
  • React Native version: 0.59.4
  • Platform(s) (iOS, Android, or both?): iOS
  • Device info (Simulator/Device? OS version? Debug/Release?): iPhone X 12.2 simulator in Xcode 10.1
iOS accepteenhancement 馃搶 pinned

Most helpful comment

I went ahead and assigned @yogevbd because this is something we are also running into.

All 12 comments

When using text for the buttons, they also have the full tappable area that native options do:

leftButtons: [
  {
    id: CLOSE_ID,
    enabled: true,
    color: "black",
    text: "Cart",
  },
],

rnn-text

I'm finding that if I make this change in RNNUIBarButtonItem.m, I get a more tappable area. However, I'm still not able to get the button to be tappable to the edge of the device.

-(instancetype)init:(NSString*)buttonId withIcon:(UIImage*)iconImage {
    UIButton* button = [[UIButton alloc] init];
    [button addTarget:self action:@selector(onButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:iconImage forState:UIControlStateNormal];
// Increase the rectangle to increase tappable area
    [button setFrame:CGRectMake(0, 0, iconImage.size.width + 40, iconImage.size.height + 20)];
    self = [super initWithCustomView:button];
    self.buttonId = buttonId;
// Force the icon to align to the left of the rectangle
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    return self;
}

rnn-tappable-improvements

I went ahead and assigned @yogevbd because this is something we are also running into.

Yeap I'm running into this one too. If you have guys have some suggestions where I can start from I can take a look.

+1 for this!

This issue can be resolved by using the built-in initWithImage instead of initWithCustomView . If you use initWithImage, you will have the tappable area like the built-in system icons so it covers the edge area.

Any reason for using initWithCustomView to set button icon?

Here is the fix:
In RNNUIBarButtonItem.m

-(instancetype)init:(NSString*)buttonId withIcon:(UIImage*)iconImage {

        // ORGINAL:
        //UIButton* button = [[UIButton alloc] init];
    //[button addTarget:self action:@selector(onButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    //[button setImage:iconImage forState:UIControlStateNormal];
    //[button setFrame:CGRectMake(0, 0, iconImage.size.width, iconImage.size.height)];
    //self = [super initWithCustomView:button];
    //self.buttonId = buttonId;

         // FIX:
    self = [super initWithImage:iconImage style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPressed)];
    self.buttonId = buttonId;

    return self;
}

The same goes for custom components, not just icons. I hope this can be addressed :) Keep up the good work guys!

Same issue here :/
I hope this will be resolved soon.

backButton has a good hitSlop, would be cool to be able to set negative insets or something for left/right buttons

@minhloi your suggestion is really good. Should be changed in the source for sure

The solution from @minhloi worked for me as well.

PR at #6482

Was this page helpful?
0 / 5 - 0 ratings