我正在将UISearchController
附加到iOS 13上的navigationItem.searchController
上。这很好:我可以使用漂亮的iOS 13样式的搜索栏。
但是,我希望默认情况下看到大标题和searchBar。
我设置navigationItem.hidesSearchBarWhenScrolling = false
是因为我想在屏幕上永久看到搜索,但是默认情况下搜索栏会替换大标题。
有人知道这怎么可能吗?
navigationItem.searchController = UISearchController(searchResultsController: nil)
navigationItem.hidesSearchBarWhenScrolling = false
答案 0 :(得分:2)
对我来说,在viewDidLoad()方法中添加以下几行后,它起作用了:
searchController.hidesNavigationBarDuringPresentation = true
navigationController?.navigationBar.prefersLargeTitles = true
navigationController!.navigationBar.sizeToFit()
答案 1 :(得分: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 = self
和navigationItem.searchController = searchController
。
对我来说,经过大量的试验和错误,它可以完美地工作,因为我是以编程方式进行所有操作的。
答案 2 :(得分: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之间的直接约束。我知道也许这不是解决方案,但我在情节提要中做了。
答案 3 :(得分: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
}
}