iOS searchbar 相關筆記

weixin_34232744發表於2015-12-18

#pragma - mark- 開始的時候

/* 開始 */

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {

/* 右邊的那個按鈕 */

searchBar.showsCancelButton = YES;

[self.searchBarView setHidden:NO];

/* 開始進入編輯狀態的時候讓返回鍵隱藏 */

[self.backButton setHidden:YES];

#pragma mark - 改變搜尋按鈕的title  不明白***************

for(id cc in [searchBar.subviews[0] subviews])

{

if([cc isKindOfClass:[UIButton class]])

{

UIButton *button = (UIButton *)cc;

[button setFrame:CGRectMake(0, 0, 100 * HEIGHT, 40 * HEIGHT)];

[button setTitle:@"取消" forState:UIControlStateNormal];

[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

}

}

return YES;

}

#pragma mark - 實現取消的點選事件

- (void)buttonClick:(UIButton *)button {

[self.searchBarView setHidden:YES];

/* 點選取消鍵讓返回鍵顯示 */

[self.backButton setHidden:NO];

}

#pragma mark - 結束

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {

/* 讓右邊取消的按鈕隱藏 */

searchBar.showsCancelButton = NO;

}

#pragma mark - 點選搜尋按鈕的時候

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

self.keyWord = self.searchBar.text;

ZYHPostContentViewController *post = [[ZYHPostContentViewController alloc] init];

post.titleContent = self.searchBar.text;

[self.navigationController pushViewController:post animated:YES];

}


#pragma mark - 點選取消按鈕的時候

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

/* 清除讓search裡面的字 */

self.searchBar.text = @"";

/* 鍵盤迴收 放棄第一響應 */

[searchBar resignFirstResponder];

[self.backButton setHidden:YES];

}

#pragma mark - 點選空白處的時候

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event {

self.searchBar.text = @"";

[self.searchBarView setHidden:YES];

}

相關文章