光标出现在定制的UITextField背景图像之前

我有一个大小为360X44的背景图像,我试图把它作为我的UITextField的背景,但是它显示出一个意想不到的行为。 在图像开始之前光标正在出现,但写入的文本正在写入。 请帮帮我。

背景的图像

实现我的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背景图像像我的波纹pipe代码和工作良好: –

  [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]; 

看起来像”-

在这里输入图像说明

我build议不要添加图像作为背景颜色。 添加它作为一个不同的组件。 即首先添加一个UIImageView ,然后添加你的UITextField

同时尝试在当前的代码中进行此更改:

 UITextField *textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(100, 75, 360, 44)]; 
 UITextView *textView = [[UITextView alloc]initWithFrame: window.frame]; textView.text = @"text\n text\n text"; UIImageView *imgView = [[UIImageView alloc]initWithFrame: textView.frame]; imgView.image = [UIImage imageNamed: @"myImage.jpg"]; [textView addSubview: imgView]; [textView sendSubviewToBack: imgView]; [window addSubview: textView]; 

不要使用背景属性。 添加图像作为子视图,并发送子视图回来。