UITextField – 为leftView添加UIView – 无法获得焦点

在我的应用程序的UITextFields有占位符文本定义(在界面生成器),我不能让这些领域获得焦点(即显示键盘,并允许编辑),当我点击占位符文本占用的区域。 如果我点击占位符文本区域以外的文本区域(虽然仍然在文本文件本身的范围内),它将正常运行(即popup键盘,我可以编辑文本区域的内容)。 我怎样才能解决这个问题?

谢谢。

编辑1

好的,我想我已经明白了。 我还设置了这些UITextFields的“leftView”属性的空白视图。 如果我删除它,可以触摸占位符文本区域中的UITextFields,并按预期做出反应; 尽pipe我需要这个视图。 如果你把这个spacer视图的背景颜色改成红色的,你可以看到它根本不挡道,所以我不知道发生了什么问题。

为什么这个代码会导致这个问题?

谢谢。

+(UIView*)getTextFieldLeftSpacerViewWithBackgroundColor:(UIColor*)backgroundColor andHeight:(CGFloat)height { UIView *leftWrapper = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 8.0f, height)]; leftWrapper.autoresizingMask = UIViewAutoresizingNone; [leftWrapper setOpaque:YES]; if(backgroundColor){leftWrapper.backgroundColor = backgroundColor;} else{leftWrapper.backgroundColor = [UIColor clearColor];} return [leftWrapper autorelease]; } +(void)setTextFieldLeftSpacerForTextFieled:(UITextField*)textField { if(textField) { UIView *spacer = [MYViewController getTextFieldLeftSpacerViewWithBackgroundColor:nil andHeight:textField.bounds.size.height]; textField.leftView = spacer; textField.leftViewMode = UITextFieldViewModeAlways; } } 

刚刚遇到同样的问题,并不想要子类,只需要使用:

 leftWrapper.userInteractionEnabled = NO; 

我放弃了这个方法。 我select了UITextField的子类,并为UITextField中的文本边界提供偏移CGRects,而不是使用不可见的视图来弥补文本。 以下SOpost非常有帮助:

缩进UITextField中的文本