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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36883946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:46:00  来源:igfitidea点击:

iOS Programmatically Add Left image to UITextField in Xcode?

iosobjective-cxcodeswiftuitextfield

提问by rose lovely

**

**

iOS Programmatically Adding Left image to UITextField in Xcode?

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

**

**

1) I want Take a UIviewwith White Background color, inside the Viewi added 4 Textfield's?

1)我想以一个UIviewWhite Background color,里面View添加的我 4 Textfield's

2) I want to add 4 different imagesto the left sideof the 4 different textfieldsinside a ViewHaving following properties?

2)我想添加4 different imagesleft side了的4 different textfields内部View具有以下特性?

Properties For Viewi) corner Radius---5ii) Border Color----light Gray Color

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

3) How to add Spacebetween the imageand Placeholder Textinside the Textfieldsfrom the below figure and also add layerfor all textfieldswith light gray color?

3)如何添加Space之间imagePlaceholder Text内部的Textfields从下面的图中,也add layerall textfieldslight gray color

How can i Achieve this ????

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

The View Combined With Textfieldsand Images like this

视图Textfields与这样的图像相结合

回答by Anbu.Karthik

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;

choice -2

选择-2

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

Swift

迅速

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

Update

更新

for TextField1

用于文本字段 1

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);

for TextField2

对于 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);

for TextField3

对于 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);

for TextField4

对于 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);

回答by Prashant Tukadiya

UITextfieldhave property that allow to set Left View you can use this for your goal

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

like this

像这样

[yourTextfield setLeftView:YOURVIEW];

回答by 4oby

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

the CGRectMake(0.0f, 0.0f, 15, 15)part lets you specify the position and size of your image (xPos, yPos, width, height)

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