UITextField 文本与清除按钮重叠
我在视图中添加一个文本字段,如下所示:
UITextField* tf_email = [[UITextField alloc] initWithFrame:CGRectMake((320-btnImage1.size.width)/2, 170, 175, 35)];
[tf_email setBackgroundColor:[UIColor clearColor]];
[tf_email setBorderStyle:UITextBorderStyleRoundedRect];
[tf_email setClearButtonMode:UITextFieldViewModeWhileEditing];
[tf_email setReturnKeyType:UIReturnKeyDone];
[tf_email setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[tf_email setEnablesReturnKeyAutomatically:NO];
[tf_email setDelegate:self];
[tf_email setOpaque:YES];
tf_email.tag=1;
tf_email.font = TTSTYLEVAR(font);
tf_email.layer.cornerRadius = 10;
tf_email.keyboardType = UIKeyboardTypeEmailAddress;
[tf_email setAutocorrectionType:UITextAutocorrectionTypeNo];
tf_email.placeholder = @"[email protected]";
[self.view addSubview:tf_email];
当我在该字段中输入长文本时,文本和清除按钮重叠。有谁知道如何解决这个问题?
I am adding a textfield to my view like this:
UITextField* tf_email = [[UITextField alloc] initWithFrame:CGRectMake((320-btnImage1.size.width)/2, 170, 175, 35)];
[tf_email setBackgroundColor:[UIColor clearColor]];
[tf_email setBorderStyle:UITextBorderStyleRoundedRect];
[tf_email setClearButtonMode:UITextFieldViewModeWhileEditing];
[tf_email setReturnKeyType:UIReturnKeyDone];
[tf_email setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[tf_email setEnablesReturnKeyAutomatically:NO];
[tf_email setDelegate:self];
[tf_email setOpaque:YES];
tf_email.tag=1;
tf_email.font = TTSTYLEVAR(font);
tf_email.layer.cornerRadius = 10;
tf_email.keyboardType = UIKeyboardTypeEmailAddress;
[tf_email setAutocorrectionType:UITextAutocorrectionTypeNo];
tf_email.placeholder = @"[email protected]";
[self.view addSubview:tf_email];
When I enter long text in to this field, the text and the clear button overlaps. Does any one know how to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建
UITextField
的子类并重写下面给出的方法,干杯!
Create a subclass of
UITextField
and override the methods given below,Cheers !!!
我想到了问题所在。在另一个文件中,我为 UITextField 定义了一个类别。该类别指定文本区域非常靠近左右边框。这导致了重叠。
经验教训:定义类别时,我们应该使用单独的文件,以便可以轻松检测到修改。
I figured what the issue was. In another file, I had a category defined for UITextField. This category, specified the text area to be very close to the left and right border. That was causing the overlap.
Lesson learnt: When defining categories, we should use separate files so that the modifications are easily detectable.
@Augustine PA,我认为更好的方法是调用 super 并修改结果,如下所示:
@Augustine P A, I think a better approach is to call the super and modify the result like this:
添加文本字段后,调用
[button BringSubviewToFront:self.view];
After adding the text field, call
[button bringSubviewToFront:self.view];