UISearchBar的詳細使用

// UISearchBar的常用方法 搜索框
UISearchBar *oneSearchBar = [[UISearchBar alloc] init]; oneSearchBar.frame = CGRectMake(0, 0, 320, 70); // 設置位置和大小 oneSearchBar.keyboardType = UIKeyboardTypeEmailAddress; // 設置彈出鍵盤的類型 oneSearchBar.barStyle = UIBarStyleBlackTranslucent; // 設置UISearchBar的樣式 oneSearchBar.tintColor = [UIColor redColor]; // 設置UISearchBar的顏色 使用clearColor就是去掉背景 oneSearchBar.placeholder = @"請輸入:"; // 設置提示文字 oneSearchBar.text = @"呵呵"; // 設置默認的文字 oneSearchBar.prompt = @"提示信息"; // 設置提示 oneSearchBar.delegate = self; // 設置代理
oneSearchBar.showsCancelButton = YES; // 設置時候顯示關閉按鈕 // oneSearchBar.showsScopeBar = YES; // 設置顯示范圍框 // oneSearchBar.showsSearchResultsButton = YES; // 設置顯示搜索結果 // oneSearchBar.showsBookmarkButton = YES; // 設置顯示書簽按鈕

[self.view addSubview:oneSearchBar]; // 添加到View上 [oneSearchBar release], oneSearchBar = nil; // 釋放內存

////////////////////////一些常用的代理方法//////////////////////////////

pragma mark - 實現取消按鈕的方法

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { NSLog(@"您點擊了取消按鈕"); [searchBar resignFirstResponder]; // 丟棄第一使用者 }

pragma mark - 實現鍵盤上Search按鈕的方法

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { NSLog(@"您點擊了鍵盤上的Search按鈕"); }

pragma mark - 實現監聽開始輸入的方法

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { NSLog(@"開始輸入搜索內容"); return YES; }

pragma mark - 實現監聽輸入完畢的方法

-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar { NSLog(@"輸入完畢"); return YES; }

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容