Cannot set searchBar as firstResponder
I noticed this issue too. What seems to happen is the call to becomeFirstResponder
is done when the searchController is still 'loading'. If you comment out becomeFirstResponder
you notice that there is no difference. So we need a way to call becomeFirstResponder after the searchController is 'done' loading.
When I looked at various delegate methods I noticed there is a delegate method:
- (void)didPresentSearchController:(UISearchController *)searchController
This method is called right after the searchController has been presented. Then I make the call to becomeFirstResponder
:
- (void)didPresentSearchController:(UISearchController *)searchController
{
[searchController.searchBar becomeFirstResponder];
}
This fixes the problem. You will notice that when the searchController is loaded, the searchbar now has focus.
Make Search Bar Become First Responder
Making the UISearchBar
the first responder on the main thread was the solution.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
UISearchController Needs an extra tap to become first responder
try this,
Just replace this line,
searchController.becomeFirstResponder()
With this below,
searchController.searchBar.becomeFirstResponder()
Edit,
func didPresentSearchController(_ searchController1: UISearchController) {
searchController1.searchBar.becomeFirstResponder()
}
Implement this delegate method and try.
UISearchBar cannot become first responder after UITableView did re-appear
I solved it by going Xcode > Product > Analyze
.
I simply forgot to add
[super viewWillAppear:(BOOL)animated];
inside -(void)ViewWillAppear:(BOOL)animated
on the corresponding UITableViewController
.
Hopefully, my mistake can save someone's time in the future.
UISeachController Delegate methods not called, searchbar can't become first responder
The problem is you are trying to access UI element(searchbarcontroller) before UI is completely loaded.
This can be done in 2 ways
Use main queue to show keypad
private func setupSearchController() {
self.navigationItem.searchController = searchController
searchController.definesPresentationContext = true
searchController.delegate = self
searchController.isActive = true
searchController.searchBar.delegate = self
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
With this approach keypad will be only show at one time in viewDidLoad
Show keypad in viewDidAppear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
With this approach keypad will be always shown whenever screen appears.
Change table when search bar becomes first responder - swift
For this, I would recommend looking at the other methods in UISearchBarDelegate
, specifically searchBarTextDidBeginEditing()
and searchBarTextDidEndEditing()
. In each of these delegate methods you can call [self.tableView reloadData]
to update all the cells.
Also, another way to implement the FriendRequestsTitleCell
would be to add a custom header view to your table view, since the cell you're using only ever appears at the top of the table view.
UISearchBar resigns first responder when trying to select UICollectionViewCell
@nayem's comment made me think of the searchBarShouldEndEditing
delegate method. The search bar does send this message prior to resigningFirstResponder
and I can return false
. Problem solved.
Related Topics
Rotation Behaving Differently on iOS6
Save Image Data to SQLite Database in Iphone
How to Resize an Image or Done as a Nsattributedstring Nstextattachment (Or Set Its Initital Size)
Add a Running Countup Display Timer to an iOS App, Like the Clock Stopwatch
My App Inside Imessage Uiactivityviewcontroller
Spritekit - Not Loading @3X Images from Sktextureatlas
Uitableview Checkmarks Disappear When Scrolling
Error: Invalid Bitcode Version (Producer: '800.0.35.0_0' Reader: '703.0.31_0')
Offscreen Uitableviewcells (For Size Calculations) Not Respecting Size Class
Why Doesn't My Cordova/Phonegap iOS App Rotate When the Device Rotates
Xcode 7.3.1 with iOS 10 Support
Detect iOS Application About to Delete
How to Show Alertview with Activity Indicator
Using Secrandomcopybytes in Swift