Make Uisearchcontroller Search Bar Automatically Active

Make UISearchController search bar automatically active

Try calling

[self.searchController setActive:YES]

before

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

How to auto enable Search bar in UISearchController when that page appear?

Try to add to viewWillAppear self.searchController.searchBar.becomeFirstResponder()

Show keyboard automatically when UISearchController is loaded

BecomeFirstResponder is the way to go, but you should do it not in viewDidLoad. Look at following discussion for details - Cannot set searchBar as firstResponder

UISearchController - search bar should be visible always

I am not quite following your logic there, but all I can say is that you can implement this really easy.

Solution

  • On your Interface Builder create a UIView will be the container
    of the searchController's searchBar and set the constraints as
    following:

Sample Image

  • Connect that UIView to the UIViewController and name it searchBarContainer
  • Create a property of UISearchController that you want to use: var searchController: UISearchController! = nil
  • Add searchController.searchBar into the searchBarContainer

Luckily your controller should look like this: (Note that here I have not implemented the delegates here but you can take care of that as this is not related to the animation :))

class SearchViewController: UIViewController {
var searchController: UISearchController! = nil
@IBOutlet weak var searchBarContainer: UIView!

// MARK: - View Lifecycle

override func viewDidLoad() {
super.viewDidLoad()

searchController = UISearchController(searchResultsController: nil)
self.searchBarContainer.addSubview(searchController.searchBar)
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

// Setup the frame each time that the view appears in order to fit the width of the screen
// If you use autolayout just call searchController.searchBar.layoutIfNeeded()
var frame = searchController.searchBar.bounds
frame.size.width = self.view.bounds.size.width
searchController.searchBar.frame = frame
}
}

Output

My Solution

Sample Image

iOS Contacts

Sample Image

UISearchBar of UISearchController moves off screen when active

Updates

After further testing I decided to just implement the UISearchBar delegate and stop using the UISearchController since I was just reusing my UICollectionView for the results. Found a great guide on how three different methods to implement the SearchBar.

Edge Case #1

TLDR

Enable clips to bounds on the containing view. Or if you have a collection view cell that has a user-generated content view, enable clips to bounds on that content view.

Details:

An additional very important fact is that you may have the search bar embedded in a container view. This can cause issues if the container view does not have clips to bounds enabled. Other examples of this being a problem is the user-generated content views in UICollectionViewCells. Compare the setting on a UITableViewCell auto-generated content view to observe the difference.

Summary

A Boolean value that determines whether subviews are confined to the
bounds of the view. Declaration

var clipsToBounds: Bool { get set } Discussion

Setting this value to true causes subviews to be clipped to the bounds
of the receiver. If set to false, subviews whose frames extend beyond
the visible bounds of the receiver are not clipped. The default value
is false.

Swift 4.x Solution

In my case, this was happening both inside a UICollectionView and inside of a UIStackView. I was able to keep my searchBar in place after noticing the following info in the searchBar quick help.

Summary

The search bar to install in your interface. Declaration

var searchBar: UISearchBar { get } Discussion

Before presenting your searchable content, install the search bar in
this property somewhere into your view controller’s interface. The
search bar becomes the starting point for searching your contents.
Interactions with the search bar are handled automatically by the
UISearchController object, which notifies the object in the
searchResultsUpdater property whenever the search information changes.
To use a custom subclass of UISearchBar, subclass UISearchController
and implement this property to return your custom search bar.

After that I was able to resolve the issue successfully by doing the following:

let searchController = UISearchController(searchResultsController: nil)
var searchBar:UISearchBar!

Sometime later I assign the searchBar from the searchController to the searchBar in my UIViewController...

BTW - I embed my searchBar in a container UIView that has all the contraints I need to keep it in place.

func setupSearchController<T:UIViewController>(delegate vc:T) where T: UISearchResultsUpdating, T:UISearchBarDelegate {
searchBar = searchController.searchBar //this is the key
searchController.searchResultsUpdater = vc
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search".localized()
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.delegate = vc
searchController.searchBar.showsCancelButton = true
definesPresentationContext = true
self.searchMenu.addSubview(searchBar)
//searchMenu is a constrained UIView in my hierarchy
}


Related Topics



Leave a reply



Submit