UITextField背景图像在iOS中不工作
我需要在UITextField中添加背景图像,左视图模式也显示图像。我已经添加了这两个功能及其工作原理。但是,我的文本内容是固定在UITextField底部的。我需要在文本字段和内容之间留出空间 下面是我的代码和图片UITextField背景图像在iOS中不工作,ios,objective-c,uitextfield,Ios,Objective C,Uitextfield,我需要在UITextField中添加背景图像,左视图模式也显示图像。我已经添加了这两个功能及其工作原理。但是,我的文本内容是固定在UITextField底部的。我需要在文本字段和内容之间留出空间 下面是我的代码和图片 UITextField *textField = [UITextField new]; textField.borderStyle = UITextBorderStyleNone; textField.background = [UIImage imageNamed:@"form
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];
在我的项目中,我解决了这个问题,使用UIImageView作为textfield的背景,并使用auto layout附加UIImageView框架以匹配UITextField的框架。完成后,您可以通过更新约束自定义textfield和UIImageView之间的边距。您必须设置textfield边框样式方形,以便将文本字段中的图像保留为背景,如下所示: 对于广场:-
[textfield setBorderStyle:UITextBorderStyleLine];
用于设置背景图像
[textField setBackground:[UIImage imageNamed:@"imagename.jpg"]];
设置phoneImageView帧的CGRectInset只需重新使用前一行中定义的帧。这是真的吗?更新CGRectMakeMake会更简单。我使用自动布局设置uitextfield的框架。CGRectInset只是在左侧图像和文本之间留出空间。这对我来说很有效,在Xcode 12上升级到iOS 14后图像不会显示,但将边框样式设置为方形就成功了。当升级到iOS 14后边框图像不显示时,这对我来说很有效!