adit
adit

Reputation: 33644

UIButton tapable area outside of subview

So I have a UIView which I call ctrView and this ctrView is added to my UIViewController's view. It's basically just a smaller rectangular frame inside. And I added a UIButton on the top right corner of this container view, however it seems that when clicking on the UIButton area which is a bit outside of the ctrView doesn't trigger the action tied to the button. Only the UIButton area that is inside the container view triggers the action. How do I resolve this weird issue?

UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [closeButton setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];
    [closeButton setBackgroundColor:[UIColor clearColor]];
    [closeButton addTarget:self action:@selector(closeZoomedImageView) forControlEvents:UIControlEventTouchUpInside];
    [closeButton setCenter:CGPointMake(ctrView.frameX + ctrView.frameWidth - 30, ctrView.frameY - 15)];
    [ctrView addSubview:closeButton];
    [closeButton release];

Upvotes: 1

Views: 790

Answers (1)

Omar Abdelhafith
Omar Abdelhafith

Reputation: 21221

This is the normal behavior, views outside the bound of the parent will not receive touch actions In order to fix this, rearrange the views like following

  • Main view will be big enough to accomodate the internal view and the closebutton, the backGroundColor of this view will be clear color
  • Internal view will be the view that will the internal view that will have the image and or the content of your normal view

Main view will have both the internal view and the close button as its childern Main view will be big enough to accomodate the button Main view will have a clear color background so that it will not affect the layout

So you will add all the views you currently have inside the internal view, The internal view and the button view will both be added to the main view

you present then the main view to the viewcontroller

Upvotes: 4

Related Questions