Less active area for custom UIBarButtonItem

I have a UINavigationBar with a custom UIBarButtonItem (which uses UIButton as a custom view). The problem is that the active area of ​​the user button is too large, if I click at least 40 pixels outside the button, it is still registered as a button click. This leads to random taps. How to reduce the active area on these buttons?

+5
source share
3 answers

I also noticed this oddity. I found that using the UIView container fixes this. For instance:

UIButton *menuButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[menuButton addTarget:self action:@selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];
[menuButton setImage:[UIImage imageNamed:@"menuIcon"] forState:UIControlStateNormal];
UIView *menuButtonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[menuButtonContainer addSubview:menuButton];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuButtonContainer];
+6
source

, ... ... , , "Command + 3" "tools → Inspector Size", "W" "H"... , ...

~ Raviraja

0

Builder ? :

yourButton.bounds = CGRectMake( 0, 0, yourImage.size.width, yourImage.size.height );  

, , :

    UIImage *image = [UIImage imageNamed:@"audio-off.png"];
    UIButton *myMuteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myMuteButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );    
    [myMuteButton setImage:image forState:UIControlStateNormal];
    [myMuteButton addTarget:self action:@selector(mute) forControlEvents:UIControlEventTouchUpInside];    
    UIBarButtonItem *myMuteBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myMuteButton];   
    navBar.leftBarButtonItem = myMuteBarButtonItem;
    [myMuteBarButtonItem release];
0

All Articles