在导航栏中显示搜索栏和大标题,也无需在iOS 13上滚动

问题描述 投票:0回答:1

我将UISearchController附加到iOS 13上的navigationItem.searchController。这很好:我可以使用漂亮的iOS 13样式的搜索栏。

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

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

有人知道这怎么可能吗?

Check this out

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

这实际上是它的外观This is how it looks actually

这是我需要实现的方式(大标题和搜索栏都可见)This is how I need to implement(large title and search bar both visible)

ios13 uisearchcontroller large-title preferslargetitles
1个回答
0
投票

尝试一下,在我这边工作正常

    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
    }

对于大型导航栏,请使用此

要获得完整的应用程序导航栏支持,请在代码中添加此扩展名。

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)!]
        }
    }
}

然后简单地调用此方法

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

如果然后也无法使用,请使用此

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

另一个解决方案是在情节提要中添加一个高度为0的UIView,并在顶部设置具有安全区域的机顶盒,在底部添加UIScrollView / UICollectionView / UITableVIew或其他可滚动视图,并删除TopSafeArea和ScrollableView Top之间的直接约束。我知道也许这不是解决方案,但我在情节提要中做到了。

© www.soinside.com 2019 - 2024. All rights reserved.