3

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

Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115

3 Answers3

3

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

Community
  • 1
  • 1
MikeS
  • 3,891
  • 6
  • 35
  • 51
0

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).

Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
  • Because iOS is Developed by Apple :-). The delete button should have a superview. what you have to do is create a UIView add Button and delete button inside that. Think of it like a container for your button and delete button. Then it should work perfectly. – Rahul Vyas Sep 26 '12 at 13:24
0

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

amit soni
  • 2,183
  • 1
  • 14
  • 17