Search Bar Always Visible

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

Show search bar in navigation bar without scrolling on iOS 11

The following makes the search bar visible at first, then allows it to hide when scrolling:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if #available(iOS 11.0, *) {
navigationItem.hidesSearchBarWhenScrolling = false
}
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if #available(iOS 11.0, *) {
navigationItem.hidesSearchBarWhenScrolling = true
}
}

Using isActive didn't do what I wanted, it makes the search bar active (showing cancel button, etc.), when all I want is for it to be visible.

keeping uisearchbar always visible in uitableview

Add Navigationbar to your class and Add the searchbar to the navigation item titleView.

  self.navigationItem.titleView = self.searchController.searchBar

Another option is to use UITableView. Just place the tableView below the searchbar.

UISearchController: show results even when search bar is empty

If your searchBar is active but has no text, the underlying tableView results are shown. That's the built-in behavior, and the reason why searchResultsController is hidden for that state.

To change the behavior when search is active but not filtering, you're going to have to show the searchResultsController when it is normally still hidden.

There may be a good way to accomplish this via <UISearchResultsUpdating> and updateSearchResultsForSearchController:. If you can solve it via the protocol, that's the preferred way to go.

If that doesn't help, you're left with hacking the built-in behavior. I wouldn't recommend or rely on it, and it's going to be fragile, but here's an answer if you choose that option:

  1. Make sure your tableViewController conforms to <UISearchControllerDelegate>, and add

    self.searchController.delegate = self;

  2. Implement willPresentSearchController:

    - (void)willPresentSearchController:(UISearchController *)searchController
    {
    dispatch_async(dispatch_get_main_queue(), ^{
    searchController.searchResultsController.view.hidden = NO;
    });
    }

    This makes the searchResultsController visible after its UISearchController set it to hidden.

  3. Implement didPresentSearchController:

    - (void)didPresentSearchController:(UISearchController *)searchController
    {
    searchController.searchResultsController.view.hidden = NO;
    }

For a better way to work around the built-in behavior, see malhal's answer.

How to have UISearchBar scope bar always visible?

The UISearchDisplayController is hiding the scope bar for you.

The way around this is to subclass UISearchBar and override the implementation of setShowsScopeBar:

@interface MySearchBar : UISearchBar {

}

@end

@implementation MySearchBar

- (void) setShowsScopeBar:(BOOL) show
{
[super setShowsScopeBar: YES]; // always show!
}

@end

Then, in Interface Builder, change the class of the Search Bar you have in your view (that is associated with the UISearchDisplayController) to the new class type -- MySearchBar in this example.

how does not hide the search bar when scrolling ? SwiftUi

In SwiftUI we have to set placement parameter in searchable modifier to .navigationBarDrawer(displayMode: .always) it's makes always show search bar

.searchable(text: $searchtext, placement: .navigationBarDrawer(displayMode: .always)) {
"your code"
}

SwiftUI searchable on NavigationView always shown, hides only on scroll

So, after adding a progress view above the list view, it suddenly started working the way I want it to. The code now is minimally changed and looks like this.

var body: some View {
NavigationView {
VStack {
if /** we're doing search, I'm checking search text **/ {
ProgressView()
.padding()
}

if !movies.isEmpty {
List {
ForEach(/** movies list **/) { movie in
// movie row here
}
}
.listStyle(GroupedListStyle())
.navigationTitle("Discover")
.searchable(text: $moviesRepository.searchText, placement: .toolbar,
prompt: Text("Search...")
.foregroundColor(.secondary)
)
} else {
ProgressView()
.navigationTitle("Discover")
}
}
}
.onAppear {
// load movies
}
}

And basically after adding the progress views, it started working the way I described it in my OP and the way it worked for ChrisR



Related Topics



Leave a reply



Submit