给textfield左侧添加图片,最终效果如图:
1.我们直接给文本框添加左视图
UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
image.image = [UIImage imageNamed:@"image.png"];
text.leftView=image;
text.leftViewMode = UITextFieldViewModeAlways;
效果如下:
我们发现图片片左上角,所以我在textfield先添加一个UIView,再在视图上添加一个UIImageView,上代码
//文本框左视图
UIView *leftView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 52)];
leftView.backgroundColor = [UIColor clearColor];
//添加图片
UIImageView *headView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 30, 32)];
headView.image = [UIImage imageNamed:@"lock"];
[leftView addSubview:headView];
_passwordTextField.leftView = leftView;
_passwordTextField.leftViewMode = UITextFieldViewModeAlways;
So,就实现了最终的效果