使UISearchController搜索栏自动激活 [英] Make UISearchController search bar automatically active

查看:36
本文介绍了使UISearchController搜索栏自动激活的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个UISearchController,其搜索栏位于导航栏中,我想在加载视图时使搜索栏处于活动状态.当我说活动"时,是指出现键盘并且用户可以在不点击搜索栏的情况下键入他/她的搜索.

I've implemented a UISearchController with its search bar in a navigatiom bar and I would like to make the search bar active when the view is loaded. When I say active, I mean that the keyboard appears and the user can type his/her search without tap the search bar.

我使用以下代码初始化了UISearchController:

I initialised the UISearchController with the following code:

- (void)viewDidLoad
{
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    [self.searchController setSearchResultsUpdater:self];
    [self.searchController setDimsBackgroundDuringPresentation:NO];
    [self.searchController setHidesNavigationBarDuringPresentation:NO];
    [self.navigationItem setTitleView:self.searchController.searchBar];

    [self setDefinesPresentationContext:YES];

    [self.searchController.searchBar setDelegate:self];
    [self.searchController.searchBar setShowsCancelButton:YES];
    [self.searchController.searchBar setPlaceholder:@"City or Airfield"];

    [self.searchController.searchBar sizeToFit];
}

我试图使我的搜索控制器处于活动状态,调用 [self.searchController.searchBar成为FirstResponder] 并直接调用 searchBarSearchButtonClicked ,但没有任何效果.

I've tried to make my search controller active, call [self.searchController.searchBar becomeFirstResponder] and directly call searchBarSearchButtonClicked but nothing works.

推荐答案

尝试致电

[self.searchController setActive:YES]

之前

[self.searchController.searchBar becomeFirstResponder]

如果上述方法无效,请尝试以下操作:

If the above is not working, try something like this:

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

这篇关于使UISearchController搜索栏自动激活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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