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

时间:2016-04-27 08:08:18

标签: ios objective-c xcode swift uitextfield

**

  

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

**

1)我想在UIview我添加White Background color内部View4 Textfield's一起拍摄?

2)我想将4 different images添加到具有以下属性的left side4 different textfields的{​​{1}}?

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

3)如何在light Gray Color内的Spaceimage之间添加Placeholder Text,如下图所示Textfields add layer all textfieldslight gray color

  

我怎样才能实现这个目标?

The View Combined With Textfields and Images like this

3 个答案:

答案 0 :(得分:5)

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

<强>更新

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

for 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

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

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

答案 1 :(得分:2)

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

像这样

[yourTextfield setLeftView:YOURVIEW];

答案 2 :(得分:0)

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