Search code examples
iosuinavigationitem

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.


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