您的位置:首页 > 产品设计 > UI/UE

UITextField的leftView和rightView属性

2015-10-11 23:51 489 查看
tf = [[UITextField alloc]init];
tf.backgroundColor = [UIColor cyanColor];
tf.frame = CGRectMake(50, 50, 100, 20);
UIImageView *imagev = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
imagev.image = [UIImage imageNamed:@"nb"];
tf.rightView = imagev;
tf.rightViewMode = UITextFieldViewModeAlways;

UILabel *paddingLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 25)];
paddingLb.text = @"$";
paddingLb.textColor = [UIColor darkGrayColor];
paddingLb.backgroundColor = [UIColor clearColor];
tf.leftView = paddingLb;
tf.leftViewMode = UITextFieldViewModeAlways;//必不可少
[self.view addSubview:tf];


参考文章:http://blog.csdn.net/wanglang3081/article/details/7464943

**********************************2015年10月13日更新****************************************************************************************************************************************

之前设置的图片距离右边界没有空隙,能够增加空隙的代码

UITextField *searchBar = [[UITextField alloc]init];
searchBar.frame = CGRectMake(0, 0, self.view.bounds.size.width - 4*padding, 30);
UIImageView*searchImgV =[[UIImageView alloc]initWithImage:[UIImage imageWithName:@"searchbar_textfield_search_icon"]];
searchImgV.contentMode = UIViewContentModeCenter;
searchImgV.frame = CGRectMake(0, 0, 30, 30);
searchBar.leftView = searchImgV;


第一段代码效果:



第二段代码效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: