grey view on table search - ios

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];
}
}
}

Related

MLPAutoCompleteTextField suggestion table interaction issue

Currently I'm using this third-party library in my project https://github.com/EddyBorja/MLPAutoCompleteTextField. This library is used to show the suggestion list based on user input.
I did setup the text field like this
self.searchTextField = [[MLPAutoCompleteTextField alloc] initWithFrame:CGRectMake(0, 0, 250, 30)];
[self.searchTextField setBorderStyle:UITextBorderStyleRoundedRect];
self.searchTextField.backgroundColor = [UIColor whiteColor];
self.searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
self.searchTextField.textColor = [UIColor blackColor];
self.searchTextField.returnKeyType = UIReturnKeyDone;
self.searchTextField.placeholder = #"Enter name to search";
self.searchTextField.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchTextField.delegate = self;
self.searchTextField.autoCompleteDelegate = self;
self.searchTextField.autoCompleteDataSource = self;
Implement the Datasource protocol as below
- (NSArray *)autoCompleteTextField:(MLPAutoCompleteTextField *)textField possibleCompletionsForString:(NSString *)string {
return #[#"AAA", #"BBB", #"CCC", #"DDD"];
When I input something to the text field, the drop down list was shown but when I tap on a cell in the drop down list, the list dissappeared without completing any words in the text field.
Anyone experienced this problem please explain to me where I was wrong. Thanks in advance.
I had the same issue awhile back. I happened to be using MLPAutocomleteTextField inside a UITableView, so all the touch events were likely intercepted by the tableview.
I worked around this issue by setting the autoCompleteTableAppearsAsKeyboardAccessory of my MLPAutocomleteTextField instance to TRUE to enable enable autocomplete as a keyboard accessory. This allowed me to select my autocomplete options. Hopefully this solves your issue as well. :)
I solved it by subclassing my UITableViewCell, a little tricky but it's working for me:
#import "AutoCompleteTableViewCell.h"
#interface AutoCompleteTableViewCell()
#property (nonatomic, assign) BOOL selectedCell;
#end
#implementation AutoCompleteTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
self.selectedCell = NO;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
UIView* hitView = [super hitTest:point withEvent:event];
if (hitView != nil)
{
[self.superview bringSubviewToFront:self];
}
return hitView;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
CGRect rect = self.bounds;
BOOL isInside = CGRectContainsPoint(rect, point);
if(!isInside)
{
for (UIView *view in self.subviews)
{
isInside = CGRectContainsPoint(view.frame, point);
if(isInside)
break;
}
}
if (!self.selectedCell) {
self.selectedCell = YES;
id view = [self superview];
while (view && [view isKindOfClass:[UITableView class]] == NO) {
view = [view superview];
}
UITableView *tableView = (UITableView *)view;
NSIndexPath *indexPath = [tableView indexPathForCell:self];
[tableView.delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
}
return isInside;
}
Then delegate method is called:
- (void)autoCompleteTextField:(MLPAutoCompleteTextField *)textField
didSelectAutoCompleteString:(NSString *)selectedString
withAutoCompleteObject:(id<MLPAutoCompletionObject>)selectedObject
forRowAtIndexPath:(NSIndexPath *)indexPath;

App crash while editing a UISearchBar and dismissing UITableViewController

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.

Unable to hide keyboard on click search button in a UISearchBar

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()
}

iOS visualization of UISearchBar scope buttons change

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];
}

iPhone: UISearchBar how to search for "" String?

I want to be able to search for a String without any character in it. The problem is, that the default keyboard does not show up the search button, when there is nothing written in the UISearchBar.
Any idea?
Thanks,
Markus
Luckily, I just happened to be working on code that does exactly this. It's a bit of a hack, but you can find the UITextField that is embedded within the UISearchBar, then turn off enablesReturnKeyAutomatically:
UITextField *searchBarTextField = nil;
for (UIView *subview in searchBar.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
Daniels answer works perfect till iOS 7.
iOS 7 Update
- (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;
}
Type * and use that as a placeholder in your program for "" (nothing).
Searching for "" is like searching for nil / NULL: the query will return all entries that are empty or null.
The * is a wildcard character used in most searching markups, and it'll return all matches.
while searching a text. it is difficult to search a string without character...
so you need to work on
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
//write code here
}
it will run automatically
this is what i did
To remove keyboard upon backspace till blank:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if ([searchText isEqualToString:#""]) {
[searchBar resignFirstResponder];
}
}
you will also need to set your uitextfield within your uisearchbar to the same delegate , remember to add to this delegate (in my code's context the delegate is self)
for (UIView *view in searchBar.subviews){
if ([view isKindOfClass: [UITextField class]]) {
UITextField *tf = (UITextField *)view;
tf.delegate = self;
break;
}
}
following that add these to your delegate
- (void)searchBarCancelButtonClicked:(UISearchBar *) aSearchBar {
[aSearchBar resignFirstResponder];
}
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
[self performSelector:#selector(searchBarCancelButtonClicked:) withObject:textField.superview afterDelay: 0.1];
return YES;
}
when any of these trigger , perform your search for "" string

Resources