gpt4 book ai didi

ios - 使 UISearchController 搜索栏自动激活

转载 作者:可可西里 更新时间:2023-11-01 03:25:27 25 4
gpt4 key购买 nike

我已经在导航栏中实现了一个带有搜索栏的 UISearchController,我希望在加载 View 时激活搜索栏。当我说事件时,我的意思是键盘出现并且用户可以在不点击搜索栏的情况下键入他/她的搜索。

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

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

我尝试激活我的搜索 Controller ,调用 [self.searchController.searchBar becomeFirstResponder] 并直接调用 searchBarSearchButtonClicked 但没有任何效果。

最佳答案

试着打电话

[self.searchController setActive:YES]

之前

[self.searchController.searchBar becomeFirstResponder]

如果上述方法不起作用,请尝试以下操作:

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

关于ios - 使 UISearchController 搜索栏自动激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31274058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com