iphone - 光标出现在自定义的 UITextField 背景图像之前

标签 iphone ios uitextfield

我有一个大小为 360X44 的背景图像,我试图将其作为我的 UITextFields 的背景,但是它显示出意外的行为。光标在图像开始之前出现,但文本在写入位置写入。

image of the background

我的 UiTextField 的实现

UITextField *textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(100, 75, 215, 30)];
textFieldRounded.borderStyle = UITextBorderStyleNone;
UIImage *fieldBGImage = [[UIImage imageNamed:@"textfield_normal.png"]  stretchableImageWithLeftCapWidth:50 topCapHeight:50];
[textFieldRounded setBackground:fieldBGImage];
textFieldRounded.textColor = [UIColor blackColor];
textFieldRounded.font = [UIFont systemFontOfSize:17.0];
textFieldRounded.placeholder = @"http://";  //place holder
textFieldRounded.backgroundColor = [UIColor whiteColor];
textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo;
textFieldRounded.backgroundColor = [UIColor clearColor];
textFieldRounded.keyboardType = UIKeyboardTypeDefault;
textFieldRounded.returnKeyType = UIReturnKeyDone;
textFieldRounded.clearsOnBeginEditing = NO;
textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing;    
[self.view addSubview:textFieldRounded];    
textFieldRounded.delegate = self;

最佳答案

我只是像我的波纹管代码一样设置 TextFiled 背景图像并且工作正常:-

        [YourTextFiled setFont:[UIFont systemFontOfSize:18]];
        YourTextFiled.textAlignment = UITextAlignmentLeft;
        YourTextFiled.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
        YourTextFiled.leftViewMode = UITextFieldViewModeAlways;
        YourTextFiled.background = [[UIImage imageNamed:@"xFOEr.png"] stretchableImageWithLeftCapWidth:7 topCapHeight:17];

看起来像“-

enter image description here

关于iphone - 光标出现在自定义的 UITextField 背景图像之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14619097/

相关文章:

ios - Swift:跳转到 CollectionView 中的下一个文本字段

ios - 无法使用 DDMathParser 求解数学表达式

iphone - XCODE 4.2 中的构建文件夹位置

ios - 键盘阻塞 UITableViewCell 的 UITextField?

ios - UISlider 通过发送者和类访问

ios - UIImageView 忽略 contentMode 的变化

ios - HW kbd 无法将(null)设置为键盘焦点 iOS Xamarin.iOS 10.2.1.5

iphone - 如何在iPhone SDK中将瘦图变成胖图?

iPhone 或 iPhone 5 位置中的条件语句更新 : method

ios - 单击 UITableViewCell 时打开新的 UIViewController?