// 创建搜索框对象
UITextField *searchBar = [[UITextField alloc] init];
searchBar.frame = CGRectMake(0, 0, 300, 30);
searchBar.font = [UIFont systemFontOfSize:15];
searchBar.placeholder = @"请输入搜索条件";
searchBar.background = [UIImage imageNamed:@"searchbar_textfield_background"];
// 设置左边的放大镜图标
// UIImage *image = [UIImage imageNamed:@"searchbar_textfield_search_icon"];
// 通过initWithImage来创建初始化UIImageView,UIImageView的尺寸默认就等于image的尺寸
// UIImageView *searchIcon = [[UIImageView alloc] initWithImage:image];
// 通过init来创建初始化绝大部分控件,控件都是没有尺寸
UIImageView *searchIcon = [[UIImageView alloc] init];
searchIcon.image = [UIImage imageNamed:@"searchbar_textfield_search_icon"];
searchIcon.frame = CGRectMake(0, 0, 30, 30);
searchIcon.contentMode = UIViewContentModeCenter;
searchBar.leftView = searchIcon;
searchBar.leftViewMode = UITextFieldViewModeAlways;
自定义搜索框
最新推荐文章于 2022-12-08 17:44:38 发布