将 UITextField 添加到 UIImageView 布局问题
可能的重复:
以图像作为背景的自定义 UITextField 闪烁光标问题
我有一个 UIImageView (图像中有图案、渐变和阴影)。我添加了一个 UITextField 作为此 UIImageView 的子视图,并将 textField 背景设置为清除,以便可以看到它,代码是:
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 12, 359 , 36)];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.textColor = [UIColor whiteColor];
textField.placeholder = @"USERNAME";
textField.backgroundColor = [UIColor clearColor];
textField.font = [UIFont systemFontOfSize:15];
textField.delegate = self;
[username_background addSubview:textField];
问题是,当我开始编辑 UITextField 时,我遇到了以下布局问题:
编辑之前:
< img src="https://i.sstatic.net/GQmXM.png" alt="在此处输入图像描述">
编辑时:
如果我将 UIColor clearColor 背景更改为其他颜色,我可以解决此问题颜色,但显然我发现很难为背景获得合适的颜色。我确实尝试过这样做:
[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bkg-username.png"]];
但是,我得到的是:
Possible Duplicate:
custom UITextField with image as a background blinking cursor issue
I have a UIImageView (the image has a pattern, gradient, and shadow in it). I have added a UITextField as a subview of this UIImageView and set the textField background to clear so that it's seen through, the code is:
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 12, 359 , 36)];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.textColor = [UIColor whiteColor];
textField.placeholder = @"USERNAME";
textField.backgroundColor = [UIColor clearColor];
textField.font = [UIFont systemFontOfSize:15];
textField.delegate = self;
[username_background addSubview:textField];
The issue is that when I start editing the UITextField, I got the following layout issues:
Before editing:
When editing:
I can fix this if I change the UIColor clearColor background to some other color, but apparently I find difficulties getting the appropriate color for the background. I did try doing:
[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bkg-username.png"]];
However, what I am getting is:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 UITextField 添加为视图控制器视图上的子视图,而不是“username_background”UIImageView。我相信这应该可以解决您的问题。
Add the UITextField as a subview on the view of the viewcontroller, and not the 'username_background' UIImageView. I believe this should fix your problem.
尝试制作一个新图像用作背景,该图像仅包含原始图像中心的图案(无渐变或角)。
Try making a new image to use as the background that consists of only the pattern in the center of the original image (no gradients or corners).