简体   繁体   English

UISearchDisplayController搜索栏会不会关闭键盘?

[英]UISearchDisplayController search bar will not dismiss keyboard?

I have a table view, being used in conjunction with a UISearchDisplayController which incorporates a UISearchBar into the header of the table view. 我有一个表视图,它与UISearchDisplayController结合使用,后者将UISearchBar合并到表视图的标题中。 I am unable to dismiss the keyboard when the user taps 'Search' on the keyboard. 当用户点击键盘上的“搜索”时,我无法关闭键盘。 I have done the following in my view controller: 我已经在视图控制器中完成了以下操作:

- (void)viewDidLoad
{

    [super viewDidLoad];

    [self setTitle:[NSString stringWithFormat:NSLocalizedString(@"ViewTitle", nil), _hotspots.count]];

    // Set up view options
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {

        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 0)];

    }

    [_tableView setTableFooterView:[[UIView alloc] init]];


    // Fix for the issue where search bar would display incorrectly on iOS 7 modal iPad view
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {

        self.edgesForExtendedLayout = UIRectEdgeNone;

    }


    // Set localisable strings for interface elements
    [self.searchDisplayController.searchBar setPlaceholder:NSLocalizedString(@"PlaceholderText", nil)];


    // Hide Search Bar
    CGRect newBounds = _tableView.bounds;
    newBounds.origin.y = newBounds.origin.y + self.searchDisplayController.searchBar.bounds.size.height;
    _tableView.bounds = newBounds;


    // Set our search bar delegate methods
    [self.searchDisplayController.searchBar setDelegate:self];

}

I have also implemented the relevant delegate methods for the UISearchBar... 我还为UISearchBar实现了相关的委托方法...

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    return YES;
}

When I tap 'Search' on the keyboard, the search bar does indeed resign first responder status, but the keyboard is not dismissed. 当我点击键盘上的“搜索”时,搜索栏确实确实放弃了第一响应者的身份,但并未关闭键盘。 I can still continue to type text, but it's not entered into my text bar, and tapping the 'Search' button again does not trigger the above delegate method. 我仍然可以继续输入文本,但是没有输入到文本栏中,再次点击“搜索”按钮不会触发上述委托方法。

I have also tried replacing [searchBar resignFirstResponder] with [self.view endEditing:YES] which has had no effect. 我也尝试过用[searchBar resignFirstResponder] [self.view endEditing:YES]替换[searchBar resignFirstResponder]无效。

The strange thing is, this is behaving differently on iOS 6 to iOS 7... On iOS 6 the search button will never dismiss the search bae correctly, however on iOS 7, if i select an item within the searchDisplayController's tableview, which pushes a new view, when i return back to the view containing the searchDisplayController, the 'Search' button dismisses the keyboard correctly each time. 奇怪的是,这在iOS 6和iOS 7上的表现方式有所不同...在iOS 6上,搜索按钮将永远无法正确消除搜索提示,但是在iOS 7上,如果我在searchDisplayController的tableview中选择一个项目,则该按钮会新视图,当我返回到包含searchDisplayController的视图时,“搜索”按钮每次都会正确关闭键盘。

Can anyone enlighten me as to what is going on here? 谁能启发我有关这里发生的事情?

UPDATE: 更新:

Additional information, this is a very simple view controller, it has a table view, a data source, and the search display controller code. 附加信息,这是一个非常简单的视图控制器,它具有表视图,数据源和搜索显示控制器代码。 I am displaying it within a modal view (form sheet) on an iPad and the interface is contained within a storyboard. 我在iPad上的模式视图(表单)中显示它,并且界面包含在情节提要中。

UPDATE 2: 更新2:

To clarify, this issue is only happening on the iPad (both iOS 6 & 7) when the view is shown in a modal (form sheet' view, the keyboard is dismissed fine on an iPhone. 为了明确起见,仅当以模态(表单工作表)视图显示视图时,才在iPad(iPad 6和iOS 7)上发生此问题,而在iPhone上可以正常关闭键盘。

Use this magic method. 使用此魔术方法。 I use it all the time, the good thing is that it always work for any field, no matter where (since it's in the main windows) and which. 我一直在使用它,好处是它无论在何处(因为它在主窗口中)和哪个地方,都可以在任何领域使用。

+ (void)dismissKeyboard {
    [[YOURAPPDELEGATE window] endEditing:YES];
}

EDIT : 编辑:

Ok, got it with "Update 2" of your question. 好的,在问题的“更新2”中得到了它。 It is a behavior with form sheet not something with search field. 这是表单的行为,而不是搜索字段的行为。

look at this link : Modal Dialog Does Not Dismiss Keyboard 看一下这个链接: 模式对话框不会关闭键盘

Replace your searchBarSearchButtonClicked: delegate method with this one: 用以下代码替换您的searchBarSearchButtonClicked:委托方法:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [self.view endEditing:TRUE]; //This will dismiss the keyboard
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解雇UISearchDisplayController时如何在搜索栏中保留文本 - How to keep text in search bar when dismiss UISearchDisplayController 隐藏UISearchDisplayController的搜索栏 - Hiding search bar of UISearchDisplayController UIPopover中带有UISearchBar的UISearchDisplayController消除了键盘问题 - UISearchDisplayController with UISearchBar in UIPopover dismiss keyboard issue 当我第一次点击搜索栏时,键盘会自动关闭 - Keyboard automatically dismiss when I first tap the search bar 如何在按下UISearch栏“搜索”按钮时关闭键盘? - How to dismiss keyboard when the UISearch Bar 'Search" button is pressed? IOS 在反应站点中使用搜索栏时键盘不会关闭 - IOS Keyboard wont dismiss when using search bar in react site uisearchdisplaycontroller预加载搜索结果和搜索栏 - uisearchdisplaycontroller preload search results and search bar UISearchDisplayController表视图重叠的搜索栏 - UISearchDisplayController table view overlapping search bar iOS:在键盘和搜索栏之间的空白处点击以在搜索栏处于活动状态时关闭键盘时的回调是什么 - iOS: What is the callback when tapped on the empty space between keyboard and search bar to dismiss the keyboard when search bar is active UISearchDisplayController:取消为关闭按钮 - UISearchDisplayController: Cancel as dismiss button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM