Abhishek
Abhishek

Reputation: 368

Navigation Bar custom button

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.

Upvotes: 0

Views: 122

Answers (3)

Arun Kumar
Arun Kumar

Reputation: 154

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];
}

Upvotes: 1

andreamazz
andreamazz

Reputation: 4286

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];

Upvotes: 1

Vaisakh
Vaisakh

Reputation: 1088

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.

Upvotes: 1

Related Questions