After I clicked the cancel button the scope bar stands alongside the search bar ... but the code is:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
searchBar.showsScopeBar = YES;
[searchBar sizeToFit];
[searchBar invalidateIntrinsicContentSize];
[searchBar setShowsCancelButton:YES animated:YES];
tabellaCanzoni.tableHeaderView = self.searchDisplayController.searchBar;
}
I want it to stay below the search, not alongside!
With this code all working right:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[_barraRicerca resignFirstResponder];
_barraRicerca.text = nil;
[_barraRicerca setShowsScopeBar:YES];
[_barraRicerca sizeToFit];
isFiltered=FALSE;
[tabellaVideo reloadData];
}
Related
I have a search and i want when it's clicked to dispaly the last table with results , I did this but i get :
How can I remove that grey view ?
I was trying to use this , but no result :
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = YES;
searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchDisplayController.searchResultsTableView.hidden=NO;
[self.searchDisplayController.searchBar bringSubviewToFront:self.searchDisplayController.searchResultsTableView];
[self.search becomeFirstResponder];
}
Try this :
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
for (UIView *v in [[[controller.searchResultsTableView superview] superview] subviews]) {
if (v.frame.origin.y == 64) {
[v setHidden:YES];
}
}
}
I am using a searchBar wherein the cancel button of searchBar when clicked should remove the keyboard. I tried using resignFirstResponder but thats not working at all. Does anyone have any other way out for it?
use this line, it will help you
[self.view endEditing:YES];
Without any code in OP, try this:
[self.view endEditing:YES];
You'll need one/all of these methods:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (searchText.length == 0) {
[searchBar resignFirstResponder];
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
}
this test code it work 1000%.
In your .h file
#interface ViewController : UIViewController<UISearchBarDelegate>
set delegate in your viewDidLoad method
#property (strong, nonatomic) UISearchBar *searchBar;
- (void)viewDidLoad
{
[super viewDidLoad];
[self.searchBar setDelegate:self];
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
[self.searchBar setShowsCancelButton:YES];
return YES;
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
searchBar.text = #"";
[searchBar resignFirstResponder];
}
I have a regular TableViewController and want to add a UITextField above that will populate the TableView data. How I can programmatically place right above the cells?
Thanks
You could place the UITextField in the UITableViewController's header.
Use the UITableViewController Delegate methods to set the properties of the header.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
Alternatively, you could use a UISearchBar.
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 0)];
searchBar.delegate = self;
[searchBar sizeToFit];
tableView.tableHeaderView = searchBar;
Then, in the UISearchBar delegate methods.
#pragma mark - UISearchBar Delegate Methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
// Remove the keyboard
[searchBar endEditing:YES];
// This method filters the data based on the search text.
[self filterDataUsingSearchText:searchBar.text];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
// Remove the keyboard
[searchBar endEditing:YES];
// Clear the text on cancel
searchBar.text = #"";
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
// This method filters the data based on the search text.
[self filterDataUsingSearchText:searchText];
}
#pragma mark -
Documentation:
UISearchBar Reference
Table View Programming Guide
I am seeing a bug where my app crashes if I click the back button in a navigation controller while editing a UISearchBar embedded as the titleView of the UINavigationBar. The main VC is a UITableViewController that is pushed onto the view stack using [parentView.navigationController pushViewController:myTableView animated:YES];
Here is the code I use to create the UISearchBar in my viewDidLoad:
UISearchBar *customSearch = [[UISearchBar alloc] initWithFrame:
CGRectMake(0,0, 320, 44)];
customSearch.delegate = self;
customSearch.placeholder = #"Some placeholder text";
self.navigationItem.titleView = customSearch;
These are my delegate implementations for the UISearchBar delegate - handle search just updates the array backing the tableView and calls [self.tableview reloadData]:
- (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
searchBar.showsCancelButton = YES;
}
- (void) searchBarTextDidEndEditing:(UISearchBar *)searchBar {
searchBar.showsCancelButton = NO;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self handleSearch:searchBar];
[searchBar resignFirstResponder];
}
- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[self handleSearch:searchBar];
}
- (void)handleSearch:(UISearchBar *)searchBar {
[self updateFilteredData:searchBar.text];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
searchBar.text = #"";
[self handleSearch:searchBar];
[searchBar resignFirstResponder];
}
I don't get any information from the crash - just a sigkill. If I'm not editing the UISearchBar it works fine. I've tried resigning the first responder and it still crashes.
Update - adding filtered data
- (void) updateFilteredData: (NSString *) nameFilter {
if (nameFilter.length) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(first_name CONTAINS[cd] %#) OR (last_name CONTAINS[cd] %#)", nameFilter, nameFilter];
self.filteredData = [self.data filteredArrayUsingPredicate:predicate];
} else {
self.filteredData = self.data;
}
[self.tableView reloadData];
}
I've tried all of the following + all of them together in viewWillDisappear. They all run successfully and the searchBar reference is to a valid UISearchBar.
-(void)viewWillDisappear:(BOOL)animated {
UISearchBar *mySearchBar = (UISearchBar *)self.navigationItem.titleView;
[mySearchBar resignFirstResponder];
mySearchBar.delegate = nil;
self.navigationItem.titleView = nil;
for (UIView *view in [mySearchBar subviews] ) {
[view removeFromSuperview];
}
[mySearchBar removeFromSuperview];
[super viewWillDisappear:animated];
}
It may not be a search bar thing, I just see the crash consistently when I'm editing the search bar - It could be something with the view hiding the keyboard and trying to redraw the cells below at the same time the TableView is being deconstructed.
I am using a UISearchBar to search a list, but when I click on the search button
on the keyboard, the keyboard does not hide.
Please help me to fix this issue.
Here is my code
-(void)search:(UISearchBar*)searchbar Text:(NSString*)text
{
[copyListOfItems removeAllObjects];
if([text length] > 0) {
// [ovController.view removeFromSuperview];
searching = YES;
NSString *searchText = searchBar.text;
for(imergencyData *objtrust in aryTrustee)
{
NSRange titleResultsRange = [objtrust.strName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:objtrust];
}
NSLog(#"number of object found %d",copyListOfItems.count);
}
else
{
// [tblTrustee insertSubview:ovController.view aboveSubview:self.parentViewController.view];
[searchbar resignFirstResponder];
searching = NO;
//tblTrustee.scrollEnabled = NO;
}
[tblTrustee reloadData];
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar
{
[aSearchBar resignFirstResponder];
[self.view endEditing:YES];
}
//Method call when type text in search box
-(void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {
[self search:theSearchBar Text:searchText];
}
//method call when on search button
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBa{
[searchBa endEditing:YES];
[searchBa resignFirstResponder];
[self search:searchBa Text:searchBar.text];
}
put this line of code and try it once,
- (void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar {
[aSearchBar resignFirstResponder];
[self.view endEditing:YES];
}
(or)
EDIT:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
[self.view endEditing:YES];
}
Set first searchbar.delegate = self then in - (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar call [searchbar resignFirstRespnder]
Good Luck
You can use below delegate which is called when searchbar Search button is clicked.
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
[theSearchBar resignFirstResponder];
}
Or use this as :
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
[self keyBoradRemove];
}
- (void) keyBoradRemove
{
searchBar.text = #"";
[searchBar resignFirstResponder];
}
here searchBar is an instance of UISearchBar.
UISearchBar *searchBar;
Hope it helps you.
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
use search bar delegate as
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar1
{
[searchBar1 resignFirstResponder];
}
use this code for hide keyboard on click of search button of keyboard
- (void)searchBarTextDidBeginEditing:(UISearchBar *) bar
{
UITextField *searchBarTextField = nil;
NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f) ? bar.subviews : [[bar.subviews objectAtIndex:0] subviews];
for (UIView *subview in views)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
But before that kindly check whether you have set the search bar delegate properly.
for swift 1.2 you can touch outside or click search , in both cases keyboard will hide
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}