相关疑难解决方法(0)

无法将searchBar设置为firstResponder

我有一个searchBar我在tableviewcontroller中设置.我引用了这个类似的问题UISearchBar在UITableView重新出现之后无法成为第一响应者,但仍然无法将其设置为第一响应者.在.h文件中:

@property (strong, nonatomic) UISearchController *searchController;
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
Run Code Online (Sandbox Code Playgroud)

在视图didload中:

self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;

self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.tableView.tableHeaderView = self.searchController.searchBar;
Run Code Online (Sandbox Code Playgroud)

并在viewDidAppear:

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

当我转向视图时,searchBar会动画,但不会出现键盘.

objective-c uisearchbar first-responder ios uisearchcontroller

44
推荐指数
10
解决办法
3万
查看次数

使UISearchController搜索栏自动激活

我已经在navigatiom栏中实现了一个带有搜索栏的UISearchController,我想在加载视图时使搜索栏处于活动状态.当我说活动时,我的意思是键盘出现,用户可以在不点击搜索栏的情况下输入他/她的搜索.

我使用以下代码初始化了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];
}
Run Code Online (Sandbox Code Playgroud)

我试图让我的搜索控制器处于活动状态,通话[self.searchController.searchBar becomeFirstResponder]并直接呼叫,searchBarSearchButtonClicked但没有任何作用.

objective-c uisearchbar ios uisearchcontroller

8
推荐指数
2
解决办法
2万
查看次数