【问题标题】:Cursor appears before customized UITextField background image光标出现在自定义 UITextField 背景图片之前
【发布时间】:2018-06-02 19:59:04
【问题描述】:

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

我的 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;

【问题讨论】:

    标签: iphone ios uitextfield


    【解决方案1】:

    我只是像我的波纹管代码一样设置 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];
    

    看起来像“-

    【讨论】:

      【解决方案2】:

      我建议不要将图像添加为背景颜色。将其添加为不同的组件。即首先添加UIImageView,然后在其上添加您的UITextField

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

      UITextField *textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(100, 75, 360, 44)];
      

      【讨论】:

      • 我尝试将其扩展到 360X44,但没有成功。那么我将如何在 UIImageView 上使用 UITextField 呢?
      【解决方案3】:
      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];
      

      不要使用背景属性。将图像添加为子视图并将子视图发送到后面。

      【讨论】:

        猜你喜欢
        • 2011-09-08
        • 1970-01-01
        • 1970-01-01
        • 2011-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-15
        • 2018-07-15
        相关资源
        最近更新 更多