通过becomeFirstResponder将焦点分配给UISearchController的UISearchBar时,键盘不会出现 [英] When assigning focus via becomeFirstResponder to UISearchController's UISearchBar, the keyboard does not appear

查看:223
本文介绍了通过becomeFirstResponder将焦点分配给UISearchController的UISearchBar时,键盘不会出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很多时间在网上搜索并与其他开发者讨论这个问题无济于事。此SO帖子中描述了确切的问题(关注UISearchBar但键盘没有出现),虽然已经很多年了。

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 UISearchBar but the keyboard not appear), although it's many years old.

我最近在IB中使用了已弃用的UISearchDisplayController和UISearchBar,并切换到了UISearchController通过iOS8的代码。

I recently switched from using the deprecated UISearchDisplayController and UISearchBar in IB, and switched over to UISearchController via the code for iOS8.

然而,我得到的问题是焦点是否正确分配(你可以说,因为取消按钮动画在搜索的右侧视图加载后栏,但键盘没有显示。

The problem I'm getting however, is that focus is assigned correctly (you can tell because the cancel button animates to the right of the search bar after the view loads), however the keyboard does not show up.

这是我的代码。

.h

@property (nonatomic, strong) UISearchController *searchController;

.m

- (void)viewDidLoad {
    [super viewDidLoad];
    ...
    [self initializeSearchController];
    ....
}

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

- (void)initializeSearchController {
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.delegate = self;
    self.searchController.searchBar.delegate = self;
    [self.searchController.searchBar sizeToFit];

    [self.tableView setTableHeaderView:self.searchController.searchBar];
    self.definesPresentationContext = YES;
}

到目前为止我尝试过的事情。

The things I've tried so far.


  • 我已尝试在0.2秒延迟时调用becomeFirstResponder,如另一个SO帖子所示。

  • I've tried calling becomeFirstResponder on a 0.2 second delay, as suggested in another SO post.

我在viewDidAppear中设置了断点,并验证self.searchController和self.searchController.searchBar都是有效对象,都不是nil。

I've set a breakpoint in viewDidAppear, and verified that both self.searchController and self.searchController.searchBar are both valid objects, neither nil.

我尝试过遵守UISearchControllerDelegate并使用以下代码片段

I've tried conforming to the UISearchControllerDelegate and using the following snippet of code

此处:

- (void)didPresentSearchController:(UISearchController *)searchController {
    //no matter what code I put in here to becomeFirstResponder, it doesn't
    //matter because this is never called, despite setting the     
    //self.searchController.delegate = self AND 
    //self.searchController.searchBar.delegate = self.
}




  • 我创建了一个新的在故事板中从头开始查看,并改为使用那个,以确保我在视图中没有一些旧的searchBar残余。这也不起作用。

    • I've created a new view from scratch in storyboards, and segued to that one instead, to make sure I didn't have some old searchBar remnant in my view. This did not work either.

      我只在真实设备(iPhone 6)上测试了这个,并且它不是没有显示键盘的模拟器问题。

      I've only tested this on a real device (iPhone 6), and it's not a simulator issue of not showing the keyboard.

      我没有想法,我看到了与网络相关的每一个问题和答案。没有什么工作。

      I'm out of ideas, and I've seen every question and answer related to this one the web. Nothing is working.

      为了再次澄清发生了什么,searchBar正确地成为第一个响应者,它右侧的取消按钮动画在屏幕上证明了这一点,但是键盘没有出现,光标不会在searchBar中闪烁。

      To clarify again what's going on, the searchBar correctly becomes the first responder, the cancel button to the right of it animates onscreen proving this, but the keyboard does not appear and the cursor does not blink in the searchBar.

      推荐答案

      有同样烦人的问题。
      你会认为通过将SearchController设置为活动状态会显示搜索控制器和键盘。不幸的是,它只做第一部分。

      Had the same annoying issue. You would think that by setting the SearchController as active would both present the the search controller and the keyboard. Unfortunately, it only does the first part.

      我的解决方案


      • 在viewDidAppear中激活搜索控制器:

      • in viewDidAppear make the Search Controller active:

      override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        resultSearchController.active = true 
      }
      


    • 一旦它是活动的,在didPresentSearchController中作为第一响应者

    • once it is active, in didPresentSearchController make as first responder

      func didPresentSearchController(searchController: UISearchController) {
        searchController.searchBar.becomeFirstResponder() 
      }
      


    • 这篇关于通过becomeFirstResponder将焦点分配给UISearchController的UISearchBar时,键盘不会出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆