Ankit Srivastava
Ankit Srivastava

Reputation: 12405

Trouble with UIButton not accepting clicks on half the area.... View Hierarchy issues..?

I have a button place on a view partially... its one half is over the view and the other half is just sort of floating. So if A is my base view and B is placed over it and I have this button added over B. If i click on the button on the part which comes above B it receives the touch and performs the action, but if this touch is outside the area of B than the touch is not performed.... what am I doing wrong here... ? Any help is appreciated.

here is how I am adding the button.. I am trying to dynamically delete buttons similar to the way the apps are deleted in IOS.

UIButton *deletButton1 = [UIButton buttonWithType:UIButtonTypeCustom] ;
        deletButton1.frame = CGRectMake(25, -5, 30,30);;
        deletButton1.backgroundColor = [UIColor clearColor];
        deletButton1.tag=1;
        UIImage *buttonImageNormal1 = [UIImage imageNamed:@"delete.png"];
        [deletButton1 setBackgroundImage:buttonImageNormal1 forState:UIControlStateNormal];
        [deletButton1 addTarget:self action:@selector(deleteButton:) forControlEvents:UIControlEventTouchUpInside];

[self.addButton1 addSubview:deletButton1]; 

Here is a screen shot..

enter image description here

Upvotes: 3

Views: 2704

Answers (3)

amit soni
amit soni

Reputation: 2163

you are adding the delete button on addbutton1 so set the background color of addbutton1 and set another color of deletebutton1 and check weather frame is proper or not. i guess your frame is not proper thats why it is happening and make sure your deletebutton1 is not getting hide because of another view.

i am not sure for this but check it once may be it will help you out. [deletButton1 setNeedsDisplay];

Upvotes: 0

MikeS
MikeS

Reputation: 3921

For the record, a subview doesn't HAVE to be inside the parent view's frame to receive touches if you override the proper methods of the parent view.

See this thread for more detail:

Interaction Beyond Bounds of UIView

Upvotes: 3

Rahul Vyas
Rahul Vyas

Reputation: 28740

The button's frame should be started from origin (0,0). I mean button should be completely inside View B. Otherwise the cutted area is not able to receive touch.Make button frame to (25,0,30,30).

Upvotes: 0

Related Questions