iphone - UIButton receiving touch events outside bounds when set as leftView of UITextField -
this has been bit of head scratcher me. in app i'm building using uitextfield, , adding button leftview property. however, seems on ipad (both sim , on device), button receiving touches outside bounds. interfering ability of uitextfield become first responder when user touches placeholder text. seems when touching placeholder text, events being handled button instead of text field itself. strangely, appears happen on ipad; works expected on iphone.
here simple code demonstrating problem:
- (void)viewwilllayoutsubviews { [super viewwilllayoutsubviews]; uitextfield *textfield = [[uitextfield alloc] initwithframe:cgrectmake(10.0f, 10.0f, (self.view.frame.size.width - 20.0f), 35.0f)]; textfield.borderstyle = uitextborderstyleroundedrect; textfield.placeholder = @"test text"; [self.view addsubview:textfield]; uibutton *addbutton = [uibutton buttonwithtype:uibuttontypecontactadd]; [addbutton addtarget:self action:@selector(touchdown:) forcontrolevents:uicontroleventtouchdown]; [addbutton addtarget:self action:@selector(touchupinside) forcontrolevents:uicontroleventtouchupinside]; [addbutton addtarget:self action:@selector(touchupoutside) forcontrolevents:uicontroleventtouchupoutside]; textfield.leftview = addbutton; textfield.leftviewmode = uitextfieldviewmodealways; } - (void)touchdown:(id)sender { nslog(@"touchdown"); } - (void)touchupinside { nslog(@"touchupinside"); } - (void)touchupoutside { nslog(@"touchupoutside"); }
it seems touches still perceived being inside though appear outside button's bounds. getting further right, button receives uicontroleventtouchdown uicontroleventtouchupoutside.
2013-07-25 11:51:44.217 testapp[22722:c07] touchdown 2013-07-25 11:51:44.306 testapp[22722:c07] touchupinside 2013-07-25 11:51:44.689 testapp[22722:c07] touchdown 2013-07-25 11:51:44.801 testapp[22722:c07] touchupoutside
edit here example background color of button changed approximate zones trigger events above. also, checked frame of button , less 30px wide.
i sat down , spent more time on last night. subclassing uitextfield in real application, ended overriding -(id)hittest:withevent:
seen below. has been working fine far.
- (id)hittest:(cgpoint)point withevent:(uievent *)event { if ([[super hittest:point withevent:event] isequal:self.leftview]) { if (cgrectcontainspoint(self.leftview.frame, point)) { return self.leftview; } else { return self; } } return [super hittest:point withevent:event]; }
Comments
Post a Comment