Iphone 检测空UITextField上的触摸事件

Iphone 检测空UITextField上的触摸事件,iphone,cocoa-touch,ios,Iphone,Cocoa Touch,Ios,当触摸没有文本的UITextField时,它不会获得第一响应者。有办法解决这个问题吗 textField = [[UITextField alloc] initWithFrame:CGRectMake(margin, 0, frame.size.width - 2 * margin, frame.size.height)]; textField.font = font; textField.backgroundColor = [UIColor clearColor];

当触摸没有文本的UITextField时,它不会获得第一响应者。有办法解决这个问题吗

    textField = [[UITextField alloc] initWithFrame:CGRectMake(margin, 0, frame.size.width - 2 * margin, frame.size.height)];
    textField.font = font;
    textField.backgroundColor = [UIColor clearColor];
    textField.textColor = [UIColor blackColor];
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.placeholder = caption;
    textField.userInteractionEnabled = YES;
    [self addSubview:textField];

您在这里子类化什么样的视图?如果它是UITableViewCell,您应该执行
[self.contentView addSubview:textField]


如果不是这个问题,请检查UITextField中是否存在透明的同级子视图重叠部分。一种简单的方法是临时设置类似于
siblingView.backgroundColor=[UIColor redColor]用于视图层次结构中UITextField的每个同级。

应该没有问题。。。。。但是你可以检查textField的userInteraction。我想你在IB中禁用了它的可编辑属性。检查它。我在代码中这样做,我没有禁用任何东西-我可以通过在一开始就触摸得到第一个响应者-但不是textField的整个帧。因此,如果我在文本字段的10%中有文本,只有文本字段的那一部分响应触摸。你能发布创建文本字段并将其添加到superview的代码吗?我们需要知道什么是
frame.size.width-2*margin
只是一个普通的UIView,没有任何重叠。我完全按照你的建议做了,文本字段显示正确,没有任何重叠视图。单击红色区域不会使文本字段获得第一响应者:/Hmm。。。尝试实现此方法以查看哪些子视图正在为每次触摸获取事件:
-(UIView*)命中测试:(CGPoint)pointwithevent:(UIEvent*)事件{UIView*recipient=[super-hitTest:pointwithevent:event];NSLog(@“recipient:%@”,recipient);return recipient;}