- 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;
第一段代码效果:
第二段代码效果: