13

我在 iOS 13 上附加了一个UISearchControllernavigationItem.searchController这很好用:我可以使用漂亮的 iOS 13 风格的搜索栏。

但是,我希望默认看到大标题和 searchBar。

我之所以设置navigationItem.hidesSearchBarWhenScrolling = false是因为我想在我的屏幕上永久看到搜索,但默认情况下搜索栏会替换大标题。

有谁知道这怎么可能?

看一下这个

navigationItem.searchController = UISearchController(searchResultsController: nil)
navigationItem.hidesSearchBarWhenScrolling = false

这就是它的实际外观 这就是它的实际外观

这就是我需要实现的方式(大标题和搜索栏都可见) 这就是我需要实现的方式(大标题和搜索栏都可见)

4

4 回答 4

3

对我来说,在viewDidLoad()方法中添加以下几行后它起作用了:

searchController.hidesNavigationBarDuringPresentation = true
navigationController?.navigationBar.prefersLargeTitles = true
navigationController!.navigationBar.sizeToFit()
于 2020-04-26T23:16:24.603 回答
1

我也一直在努力为我的应用程序实现同样的目标,我终于做到了。

我想在 UITableViewController 上添加一个 searchBar,我就是这样做的。

let searchController: UISearchController = {
    let searchController = UISearchController(searchResultsController: nil)
    
    searchController.searchBar.placeholder = "New Search"
    searchController.searchBar.searchBarStyle = .minimal
    searchController.dimsBackgroundDuringPresentation = false
    searchController.definesPresentationContext = true
    
   return searchController
}()

您首先使用闭包创建一个新的 UISearchController,这样您就可以在您的代码中全局使用它,并在将来更轻松地对其进行自定义。

之后在 viewDidLoad 中,设置searchSontroller.searchResultsUpdater = selfnavigationItem.searchController = searchController

对我来说,经过大量试验和错误后它可以完美运行,因为我正在以编程方式进行所有操作。

于 2020-05-15T20:25:36.257 回答
1

Try this, working fine in my side

    private var search = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        super.viewDidLoad()
        search.searchBar.delegate = self
        search.searchBar.sizeToFit()
        search.obscuresBackgroundDuringPresentation = false
        search.hidesNavigationBarDuringPresentation = true
        self.definesPresentationContext = true
        search.searchBar.placeholder = "Search here"
        self.navigationItem.searchController = search
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationItem.hidesSearchBarWhenScrolling = false
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationItem.hidesSearchBarWhenScrolling = true
    }

For large navigation bar use this

For full application navigation bar support please add this extension inside your code.

import UIKit
extension UIViewController {


    open func showNavigationBar(_ large: Bool,
                                _ animated: Bool,
                                titleColor: UIColor,
                                barTintColor: UIColor,
                                fontSize: CGFloat) {

        navigationController?.navigationBar.barTintColor = barTintColor
        navigationController?.navigationBar.backgroundColor = barTintColor
        navigationController?.navigationBar.isTranslucent = true
        self.navigationController?.setNavigationBarHidden(false, animated: animated)
        if large {
            self.navigationController?.navigationBar.prefersLargeTitles = true
            if #available(iOS 13.0, *) {
                let appearance = UINavigationBarAppearance()
                appearance.backgroundColor = barTintColor
                appearance.titleTextAttributes = [.foregroundColor: titleColor]
                appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor,
                                                       NSAttributedString.Key.font: UIFont(resource: R.font.robotoMedium, size: fontSize)!]

                navigationController?.navigationBar.standardAppearance = appearance
                navigationController?.navigationBar.compactAppearance = appearance
                navigationController?.navigationBar.scrollEdgeAppearance = appearance
            } else {
                self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor,
                                                                                     NSAttributedString.Key.font: UIFont(resource: R.font.robotoMedium, size: fontSize)!]
            }
        } else {
            self.navigationController?.navigationBar.prefersLargeTitles = false
            self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor,
                                                                            NSAttributedString.Key.font: UIFont(resource: R.font.robotoMedium, size: 20.0)!]
        }
    }
}

And Then call this method simply

self.showNavigationBar(true, true, titleColor: UIColor.blue, barTintColor: UIColor.red, fontSize: 32.0)

If then Also not work then use this

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        search.searchBar.becomeFirstResponder()
    }

one more solution is that add one UIView with height 0 in storyboard and set-top with safe area and bottom with UIScrollView/UICollectionView/UITableVIew or something else scrollable view and Remove Direct Constraint between TopSafeArea And ScrollableView Top. I know maybe this is not a solution but I did in a storyboard.

于 2019-11-06T10:33:42.587 回答
0

这段代码应该可以工作

class NavigationController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        createCustomTabBar()
    }

    func createCustomTabBar() {
    
        let firstVC = UINavigationController(rootViewController: HomeVC())
        firstVC.title = "Home"
        firstVC.tabBarItem.image = UIImage(systemName: "house.fill")

        viewControllers = [firstVC]
    }

class HomeVC: UIViewController {
    let searchController = UISearchController(searchResultsController: nil)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        navigationController?.navigationBar.prefersLargeTitles = true
        navigationItem.title = "Home"
        navigationItem.searchController = searchController
    }
}

导航栏+搜索栏

于 2020-09-03T10:41:11.663 回答