2

I have been using IQKeyboardManager to controller the keyboard in my app. It works fine, except with UISearchBar. How do I make it work with UISearchBar?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199

4 Answers4

6

Working with UISearchBar is explicitly disabled on IQKeyboardManager.

You should modify -(BOOL)isSearchBarTextField method to return NO here IQUIView+Hierarchy.m to work with UISearchBar.

-(BOOL)isSearchBarTextField
{
    return NO;
}
Mohd Iftekhar Qurashi
  • 2,385
  • 3
  • 32
  • 44
2

I made it work going to IQUIView+Hierarchy.swift and commenting the searchBar() part (as somebody else also pointed out):

if _IQcanBecomeFirstResponder == true {
        _IQcanBecomeFirstResponder = isUserInteractionEnabled == true &&
         isHidden == false && alpha != 0.0 &&
         isAlertViewTextField() == false &&
         // searchBar() == nil
    }

Be aware that it is not recommended to modify pods directly (a major problem will be that at the next pod install your changes will be wiped).

One solution (that I also use) is to fork the project, commit your changes and modify the podfile to download IQKeyboardManager from you repo.

Teodor Ciuraru
  • 3,417
  • 1
  • 32
  • 39
  • very good approach to use your own repo! Brilliant idea! upvote I have experience with manual modification of dependencies, this is a source of nightmare for future changes, especially if someone else will work on your code. – Alex M.M. Sep 16 '19 at 13:52
1

In the file "IQUIView+Hierarchy.m", change this method: -(BOOL)_IQcanBecomeFirstResponder

Change the line:

BOOL _IQcanBecomeFirstResponder = ([self canBecomeFirstResponder] && [self isUserInteractionEnabled] && ![self isHidden] && [self alpha]!=0.0 && ![self isAlertViewTextField]  && ![self isSearchBarTextField]);

To:

BOOL _IQcanBecomeFirstResponder = ([self canBecomeFirstResponder] && [self isUserInteractionEnabled] && ![self isHidden] && [self alpha]!=0.0 && ![self isAlertViewTextField]);

You just have to delete ![self isSearchBarTextField] from the condition.

Ninjakannon
  • 3,751
  • 7
  • 53
  • 76
1

I have the same issue using IQKeyboardManagerSwift with a UISearchController.

Apparently, an issue is pending on this on the official GitHub tagged as "Need Investgation". The potential solutions mentioened are :

This won't happen if:

  • automaticallyAdjustsScrollViewInsets = false OR
  • Use UITableViewController OR
  • searchController.hidesNavigationBarDuringPresentation = false OR
  • Disable IQKeyboardManager

For me,

searchController.hidesNavigationBarDuringPresentation = false

Did the job !

Hope this helps ;)