ChatGPT解决这个技术问题 Extra ChatGPT

Show search bar in navigation bar without scrolling on iOS 11

I’m attaching a UISearchController to the navigationItem.searchController property of a UITableViewController on iOS 11. This works fine: I can use the nice iOS 11-style search bar.

However, I’d like to make the search bar visible on launch. By default, the user has to scroll up in the table view to see the search bar. Does anyone know how is this is possible?

https://i.stack.imgur.com/6I4uKm.png

Left: default situation after launch. Right: search bar made visible (by scrolling up). I’d like to have the search bar visible after launch, as in the right screenshot.

I already found that the search bar can be made visible by setting the property hidesSearchBarWhenScrolling of my navigation item to false. However, this causes the search bar to always be visible — even when scrolling down —, which is not what I want.

in where you added the code hidesSearchBarWhenScrolling
what about setting it as s firstResponder?
The selected answer below works for me on load, but I'd also like to re-display the Search Controller when programmatically scrolling to the top with scrollView.setContentOffset(_:animated). Anyone have a suggestion?
@Jonathan hidesSearchBarWhenScrolling = false puts the search bar over large title in iOS 13. Any idea if I can update this somehow?
nothing works from answers, did someone solve issue?(

G
Gobe

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.


Thanks, worked for me. Interesting solution though :)
This solution works, but there are side effects if you are also displaying a navigation bar if it was previously hidden (i.e. pushing this view w/ search bar onto the navigation stack) The search bar will appear static in place as the navigation bar animates. Looks awful :(
Not if you put the first part in viewDidLoad instead of viewWillAppear
Can someone please elaborate on why this works and why searchController.searchBar.isHidden = false in viewDidLoad does not? The latter seems far more logical to me
This causes visual bug in iOS 13 on going back from child screen.
c
craft

You can set the property isActive to true after adding the searchController to the navigationItem.

Just like this:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    searchController.isActive = true
}

No setter method 'setIsActive:'
You're right, @NikolayKrasnov. isActive is a read-only boolean. You will want to use searchController.active = true, instead.
B
Bùi Đức Khánh

For me it worked after adding following lines in viewDidLoad() method:

navigationController?.navigationBar.prefersLargeTitles = true
navigationController!.navigationBar.sizeToFit()

G
GaétanZ

On iOS 13, @Jordan Wood's answer didn't work. Instead I did:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    UIView.performWithoutAnimation {
        searchController.isActive = true
        searchController.isActive = false
    }
}

Almost! The search bar animates to become visible after the view appears. This will annoy users. :(
R
RakeshDipuna
For (iOS 13.0, *) and SwiftUI

navigationController?.navigationBar.sizeToFit()

Example:

struct SearchBarModifier: ViewModifier {
        let searchBar: SearchBar
        func body(content: Content) -> some View {
        content
            .overlay(
                ViewControllerResolver { viewController in
                    viewController.navigationItem.searchController = self.searchBar.searchController
                    viewController.navigationController?.navigationBar.sizeToFit()

                }
                .frame(width: 0, height: 0)
            )
    }
}

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now