iOS UISearchBar搜索框
#import "ViewController.h" @interface ViewController ()<UISearchBarDelegate> /** 搜索框 */ @property (nonatomic, weak) UISearchBar *searchBar; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 40)]; //设置提示文字 self.searchBar.placeholder = @"搜索"; //设置边框背景图片 self. searchBar.backgroundImage = [UIImage imageNamed:@"searchBar_BG"]; //设置代理 self.searchBar.delegate = self; //添加到视图 [self.view addSubview:self.searchBar]; } @end
就可以简单运行UISearchBar了
正式项目中往往要做成懒加载样式
同样是搜索框,我们深入一点看看UISearchBar的属性和方法