import “ViewController.h”
@interface ViewController ()
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.self.view.backgroundColor = [UIColor whiteColor];
UISearchBar * mySearchBar = [[UISearchBar alloc] initWithFrame:(CGRectMake(0, 22, [UIScreen mainScreen].bounds.size.width, 41))];
mySearchBar.delegate = self;mySearchBar.backgroundColor = [UIColor redColor];
// 控件样式
mySearchBar.barStyle = UIBarStyleDefault;
mySearchBar.keyboardType = UIKeyboardTypeDefault;
// 占位符
mySearchBar.placeholder = @”输入搜索内容”;
// mySearchBar.showsBookmarkButton = YES;
// mySearchBar.showsCancelButton = YES;
[self.view addSubview:mySearchBar];
}
pragma mark – 代理方法
(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
NSLog(@”开始输入”); // 第一步
return YES;
}(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
NSLog(@”开始编辑UISearchBar 的 textView”); // 第二步
}(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
NSLog(@”结束输入”);
return YES;
}
// 当textView的文字改变或者清除的时候调用此方法, 搜若栏目目前的状态正在编辑, 在搜索文字字段中的当前文本
- (void)searchBar:(UISearchBar )searchBar textDidChange:(NSString )searchText
{
NSLog(@”时刻显示输入框中的内容——%@”, searchText);
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
NSLog(@”结束编辑输入”);
}
(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
// 当点击search的时候调用
NSLog(@”点击搜索按钮”);
}(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
UISearchBar 属性介绍: