When assigning focus via becomeFirstResponder to UISearchController's UISearchBar, the keyboard does not appear

后端 未结 10 867
感动是毒
感动是毒 2020-12-14 07:01

I\'ve spent quite a bit of time searching online and talking to other developers about this issue to no avail. The exact issue is described in this SO post (Focus on the UIS

相关标签:
10条回答
  • 2020-12-14 07:28

    Working Solution:-

    Don't use [self.searchController setActive:YES] before becomeFirstResponder.

    - (void)viewDidAppear:(BOOL)animated {
       [super viewDidAppear:animated];
       dispatch_async(dispatch_get_global_queue(0, 0), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
           // [self.searchController setActive:YES];
            [self.searchController.searchBar becomeFirstResponder];
         });
     });
    }
    
    0 讨论(0)
  • 2020-12-14 07:30

    I had trouble with an UISearchBar not displaying the keyboard when doing

    [searchBar becomeFirstResponder];
    

    By searching on the net, i found this thread on the Apple developer website that helped me to discover that the keyboard won't open if you don't have a keyWindow.

    The application i work on do something like this :

    1. Window A (KeyWindow)
    2. do some things
    3. open Window B (KeyWindow)
    4. do some things
    5. close Window B (resign KeyWindow)

    I just had to do

    [[[[UIApplication sharedApplication] windows] firstObject] makeKeyWindow];
    

    after the resigning of window B and no more trouble with the keyboard.

    0 讨论(0)
  • 2020-12-14 07:32

    In iOS 10, I had to run the code in delegate method on main thread. First I set the active to YES in viewDidAppear,

    -(void)viewDidAppear:(BOOL)animated{
        [super viewDidAppear:animated];
          [self.searchController setActive:YES];
    }
    

    and then in the delegate method:

    - (void)didPresentSearchController:(UISearchController *)searchController
    {
      dispatch_async(dispatch_get_main_queue(), ^{
      [searchController.searchBar becomeFirstResponder];
      });
    }
    
    0 讨论(0)
  • 2020-12-14 07:37

    This might also be related to Simulator Settings. Just disable Hardware -> Keyboard -> "Connect Hardware Keyboard" .

    For further details: UISearchBar not showing keyboard when tapped

    0 讨论(0)
提交回复
热议问题