// 为键盘添加隐藏按钮 UIToolbar * backView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)]; [backView setBarStyle:UIBarStyleDefault]; UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; [btn setTitle:@"隐藏键盘" forState:UIControlStateNormal]; btn.frame = CGRectMake(2, 5, 70, 25); [btn addTarget:self action:@selector(cancelInput:) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithCustomView:btn]; NSArray * buttonsArray = @[btnSpace,doneBtn]; [backView setItems:buttonsArray]; [self.searchBar setInputAccessoryView:backView];