문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top