解决思路:
1、创建一个UIImageView,并设置好图片
2、使用UITextField的rightView = UIImageView
备注:
有一个小问题:UIImageView被添加到rightView的时候不能设置起始点,默认是处于UITextField的最右边,可能使得图片位置不好看
解决方法:
处理一下图片:1、背景透明 2、将图片加宽,右侧为透明的部分,这样就可以使得要显示的图片的位置调整到需要的位置
源码:
_searchBook.delegate = self;
_searchBook.returnKeyType = UIReturnKeySearch;
_searchBook.placeholder = @"关键字/标题/ISBN";
_searchBook.borderStyle = UITextBorderStyleRoundedRect;
_searchBook.backgroundColor = [UIColor redColor];
UIImageView *BookSearchBackgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"BookSearchBackground"]];
BookSearchBackgroundView.frame = CGRectMake(0, 0, 20, 20);
_searchBook.rightView = BookSearchBackgroundView;
_searchBook.rightViewMode = UITextFieldViewModeAlways;