Question

backButton =  [UIButton buttonWithType:UIButtonTypeCustom];

[backButton addTarget:self action:@selector(gotoAmphorasViewController) forControlEvents:UIControlEventTouchUpInside];

[backButton setFrame:CGRectMake(0.0f,0.0f, 44,44)];

The problem i am facing is that though the button dimensions are44*44, wherever i tap anywhere around it, the the button action is fired.

Was it helpful?

Solution

Please try the bellow code : its working properly

- (void)viewDidLoad {
    [super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:@"back.png"];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    [button setImage:buttonImage forState:UIControlStateNormal];


    button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

     [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    self.navigationItem.leftBarButtonItem = customBarItem;
    [customBarItem release];



}

-(void)back {

[self.navigationController popViewControllerAnimated:YES];
}

OTHER TIPS

Its not a bug. It is a default behaviour. In iPhone, for navigation bar buttons the touch detection is little more expanded rather than its frame. Just have a look on any other application. Everywhere the button get fired if we tap nearer but outside its frame.

It's the intended behaviour, if you really want to limit the area of the touch, you can wrap the button inside a UIView:

UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[buttonContainer addSubview:button];
_barButton = [[UIBarButtonItem alloc] initWithCustomView:buttonContainer];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top