【问题标题】:iOS Programmatically Add Left image to UITextField in Xcode?iOS以编程方式将左图像添加到Xcode中的UITextField?
【发布时间】:2016-08-21 09:42:12
【问题描述】:

**

iOS 以编程方式将左图添加到 Xcode 中的 UITextField?

**

1) 我想在View 中添加UIviewWhite Background color,我添加了4 Textfield's

2) 我想将4 different images 添加到4 different textfields 内的left side View 具有以下属性?

视图属性 i) corner Radius---5 ii) Border Color----light Gray Color

3) 如何在下图中的Textfields 内的imagePlaceholder Text 之间添加Space 以及all textfieldslight gray coloradd layer

我怎样才能做到这一点????

The View Combined With Textfields and Images like this

【问题讨论】:

  • 使用AutolayoutUITextField旁边添加UIImageView
  • 如果是UITableViewCell,就用cell.imageView.image stackoverflow.com/questions/4999017/…
  • @Tim007....我只添加到视图中的文本字段...不是 Tableview

标签: ios objective-c xcode swift uitextfield


【解决方案1】:
UIImageView *imgforLeft=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; // Set frame as per space required around icon
[imgforLeft setImage:[UIImage imageNamed:@"yourImagename.png"]];

[imgforLeft setContentMode:UIViewContentModeCenter];// Set content mode centre or fit 

self. yourTextfield.leftView=imgforLeft;
self. yourTextfield.leftViewMode=UITextFieldViewModeAlways;

选择-2

// short answer with out 
self. yourTextfield.leftViewMode = UITextFieldViewModeAlways;
self. yourTextfield.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourImagename.png"]];

斯威夫特

self. yourTextfield.leftViewMode = UITextFieldViewMode.Always
self. yourTextfield.leftView = UIImageView(image: UIImage(named: "yourImagename.png"))

更新

对于 TextField1

UIImageView *imgforLeft1=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; // Set frame as per space required around icon
[imgforLeft1 setImage:[UIImage imageNamed:@"yourImagename1.png"]];

[imgforLeft1 setContentMode:UIViewContentModeCenter];// Set content mode centre or fit 

self. yourTextfield1.leftView=imgforLeft1;
self. yourTextfield1.leftViewMode=UITextFieldViewModeAlways;

  self. yourTextfield1.layer.sublayerTransform = CATransform3DMakeTranslation(30, 0, 0);

对于 TextField2

UIImageView *imgforLeft2=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; // Set frame as per space required around icon
[imgforLeft2 setImage:[UIImage imageNamed:@"yourImagename2.png"]];

[imgforLeft1 setContentMode:UIViewContentModeCenter];// Set content mode centre or fit 

self. yourTextfield2.leftView=imgforLeft1;
self. yourTextfield2.leftViewMode=UITextFieldViewModeAlways;

   self. yourTextfield2.layer.sublayerTransform = CATransform3DMakeTranslation(30, 0, 0);

对于 TextField3

UIImageView *imgforLeft3=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; // Set frame as per space required around icon
[imgforLeft3 setImage:[UIImage imageNamed:@"yourImagename3.png"]];

[imgforLeft3 setContentMode:UIViewContentModeCenter];// Set content mode centre or fit 

self. yourTextfield3.leftView=imgforLeft1;
self. yourTextfield3.leftViewMode=UITextFieldViewModeAlways;

  self. yourTextfield3.layer.sublayerTransform = CATransform3DMakeTranslation(30, 0, 0);

对于 TextField4

UIImageView *imgforLeft4=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; // Set frame as per space required around icon
[imgforLeft4 setImage:[UIImage imageNamed:@"yourImagename4.png"]];

[imgforLeft4 setContentMode:UIViewContentModeCenter];// Set content mode centre or fit 

self. yourTextfield4.leftView=imgforLeft1;
self. yourTextfield4.leftViewMode=UITextFieldViewModeAlways;

  self. yourTextfield4.layer.sublayerTransform = CATransform3DMakeTranslation(30, 0, 0);

【讨论】:

  • @Anbu.Karthik...我想在文本字段的左侧添加 4 个文本文件和 4 个图像。请提供示例代码
  • @roselovely - 嗨,4 张图片相同或不同
  • ...不同文本字段的不同图像
  • @roselovely - 答案仅适用于 Objective - C ,检查更新的答案一次
  • @Anbu.Karthik...如何在文本字段内的图像和占位符文本之间添加间距
【解决方案2】:

UITextfield 具有允许设置左视图的属性,您可以将其用于您的目标

喜欢这个

[yourTextfield setLeftView:YOURVIEW];

【讨论】:

  • 一一添加 lke [yourTextfield1 setLeftView:YOURVIEW1]; [yourTextfield2 setLeftView:YOURVIEW2]; .....
  • 参考 Anbu.Karthik 的回答这样做
【解决方案3】:
@property (weak, nonatomic) IBOutlet UITextField *myTextField;


UIImageView * myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 15, 15)];
myImageView.image = [UIImage imageNamed:@"myImage"];
[myTextField addSubView:myImageView];

CGRectMake(0.0f, 0.0f, 15, 15) 部分可让您指定图像的位置和大小(xPos, yPos, width, height)

【讨论】:

  • @roselovely 重复同样的事情 4 次...但您可能会考虑使用 PKT 的答案,因为这是一种更好的方法,我忘记了默认的 leftViewrightView
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-23
  • 2020-07-16
  • 1970-01-01
  • 2010-12-02
  • 2021-01-22
相关资源
最近更新 更多