// 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 - 實現(xiàn)取消按鈕的方法
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { NSLog(@"您點擊了取消按鈕"); [searchBar resignFirstResponder]; // 丟棄第一使用者 }
pragma mark - 實現(xiàn)鍵盤上Search按鈕的方法
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { NSLog(@"您點擊了鍵盤上的Search按鈕"); }
pragma mark - 實現(xiàn)監(jiān)聽開始輸入的方法
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { NSLog(@"開始輸入搜索內容"); return YES; }
pragma mark - 實現(xiàn)監(jiān)聽輸入完畢的方法
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar { NSLog(@"輸入完畢"); return YES; }