ios - UITextField 背景图片在 iOS 中不起作用

标签 ios objective-c uitextfield

我需要在 UITextField 中添加背景图像,左 View 模式也显示图像。我已经添加了它们及其工作原理。但是我的文本内容坚持 UITextField 底部。我需要在文本字段和内容之间留出空间。

enter image description here

下面是我的代码和图片。

UITextField *textField = [UITextField new];
textField.borderStyle = UITextBorderStyleNone;
textField.background = [UIImage imageNamed:@"form-control.png"];
textField.translatesAutoresizingMaskIntoConstraints = NO;
textField.placeholder = @"Email/Phone no";
textField.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:16];

UIImageView *phoneImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 18, 21)];
phoneImageView.frame = CGRectInset(phoneImageView.frame, -5, 0);
[phoneImageView setImage:[UIImage imageNamed:@"phone.png"]];
[phoneImageView setContentMode:UIViewContentModeScaleAspectFit];
textField.leftView = phoneImageView;
textField.leftViewMode = UITextFieldViewModeAlways;

[self.view addSubview:textField];

最佳答案

您必须设置您的文本字段边框样式方 block ,以便将图像作为背景保留在您的文本字段中,如下所示:

对于方形:-

    [textfield setBorderStyle:UITextBorderStyleLine];

用于设置背景图片

  [textField setBackground:[UIImage imageNamed:@"imagename.jpg"]];

关于ios - UITextField 背景图片在 iOS 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33632173/

相关文章:

ios - IBDesignable UIView 子类总是无法在 Interface Builder 中呈现

ios - 调用异步方法的方法的单元测试

ios - 如何在 IOS swift 中创建侧边栏菜单?

iphone - 在纬度/经度列表中查找最接近的纬度和经度匹配

ios - 在 iwatch 中触摸 UITextField 外部时关闭键盘

objective-c - Xcode iOS : Calling a number from the app

ios - 在 iOS( objective-c )中单击 Button 时,如何在 UITableView 中获取特定的 JSON 数据?

ios - 从字符串变量中获取的不仅仅是 char

ios - 使用 swift 更改自定义属性检查器值

ios - 在 UITextField 的开头创建空间