Ios 导航栏上的搜索栏未显示在viewdidload()上

Ios 导航栏上的搜索栏未显示在viewdidload()上,ios,swift,Ios,Swift,我似乎无法强制嵌入在搜索控制器中的搜索栏在视图控制器最初加载时显示。我已尝试添加navigationItem.HideseArchBarWhenScrolling=false当加载时强制搜索栏出现,但这样会使标题变小。有没有办法让搜索栏显示一个大标题 下面的代码和图片显示了我的意思 import Moya import RealmSwift import UIKit class MeetingsViewController: UIViewController { @IBOutlet

我似乎无法强制嵌入在搜索控制器中的搜索栏在视图控制器最初加载时显示。我已尝试添加
navigationItem.HideseArchBarWhenScrolling=false
当加载时强制搜索栏出现,但这样会使
标题变小。有没有办法让搜索栏显示一个大标题

下面的代码和图片显示了我的意思

import Moya
import RealmSwift
import UIKit

class MeetingsViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    private var searchController: UISearchController?
    private var provider = MoyaProvider<NewLyfe>()
    private var meetings: [Meeting]? {
        didSet {
            tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupSearchController()
        setupBarItems()
        updateViews()
    }

}

// MARK: - private functions

extension MeetingsViewController {
    private func setupSearchController() {
        let storyboard = UIStoryboard(name: "Main", bundle: .main)
        let searchResultController = storyboard.instantiateViewController(withClass: SearchResultTableViewController.self)

        searchController = UISearchController(searchResultsController: searchResultController)
        searchController?.searchBar.placeholder = "neighborhood, city, state, or zip code"
        searchController?.searchBar.delegate = self
        searchController?.searchResultsUpdater = searchResultController
        navigationItem.searchController = searchController

    }

    private func setupBarItems() {
        let mapIcon = UIBarButtonItem(image: UIImage(systemName: "map.fill"), style: .plain, target: self, action: #selector(presentMapModal))
        mapIcon.tintColor = .systemGreen
        navigationItem.rightBarButtonItem = mapIcon

        let filterIcon = UIBarButtonItem(image: UIImage(systemName: "slider.horizontal.3"), style: .plain, target: self, action: #selector(presentFilterModal))
        filterIcon.tintColor = .systemGreen
        navigationItem.leftBarButtonItem = filterIcon
    }

    private func updateViews() {
        let realm = try? Realm()
        let filterOptions = realm?.objects(FilterOptions.self).first
        searchController?.searchBar.text = filterOptions?.name
        fetchMeetings(using: filterOptions)
    }

    private func fetchMeetings(using filterOptions: FilterOptions?) {
        guard let options = filterOptions, let lat = options.latitude.value, let long = options.longitude.value else { return }
        provider.request(.meetings(day: "Sun", program: "AA", region: "\(lat),\(long),10")) { (result) in
            switch result {

            case .success(let response):
                do {
                    self.meetings = try response.map([Meeting].self)
                } catch let error {
                    print("Error decoding meetings: \(error)")
                }
            case .failure(let error):
                print("Error fetching meetings: \(error)")
            }
        }
    }

}

// MARK: - @objc functions

extension MeetingsViewController {
    @objc func presentFilterModal() {
        performSegue(withIdentifier: "presentFilterModal", sender: nil)
    }

    @objc func presentMapModal() {
        performSegue(withIdentifier: "presentMapModal", sender: nil)
    }

}

// MARK: - Table view data source/delegate

extension MeetingsViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return meetings?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "meetingCell", for: indexPath)

        cell.textLabel?.text = "\(meetings?[indexPath.row].name ?? "")"
        cell.detailTextLabel?.text = "\(meetings?[indexPath.row].day ?? "")"

        return cell
    }

}

// MARK: - Search bar delegate

extension MeetingsViewController: UISearchBarDelegate {
    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        updateViews()
    }

}
导入Moya
导入RealmSwift
导入UIKit
类会议可视控制器:UIViewController{
@IBVAR表格视图:UITableView!
私有var searchController:UISearchController?
private var provider=MoyaProvider()
非公开会议:[会议]{
迪塞特{
tableView.reloadData()
}
}
重写func viewDidLoad(){
super.viewDidLoad()
setupSearchController()
setupBarItems()
updateViews()
}
}
//马克:私人职能
扩展会议可视控制器{
专用函数setupSearchController(){
让故事板=UIStoryboard(名称:“Main”,捆绑包:.Main)
让searchResultController=storyboard.InstanceEviewController(withClass:SearchResultableViewController.self)
searchController=UISearchController(searchResultController:searchResultController)
searchController?.searchBar.placeholder=“邻居、城市、州或邮政编码”
searchController?.searchBar.delegate=self
searchController?.SearchResultsUpdate=searchResultController
navigationItem.searchController=searchController
}
专用函数setupBarItems(){
让mapIcon=uiBarButtonim(图像:UIImage(系统名:“map.fill”),样式:。普通,目标:self,操作:#选择器(presentMapModal))
mapIcon.tintColor=.systemGreen
navigationItem.rightBarButtonItem=mapIcon
让filterIcon=UIBarButtonItem(图像:UIImage(系统名:“slider.horizontal.3”),样式:。普通,目标:self,操作:#选择器(presentFilterModal))
filterIcon.tintColor=.systemGreen
navigationItem.leftBarButtonItem=filterIcon
}
私有函数更新视图(){
让realm=try?realm()
让filterOptions=realm?.objects(filterOptions.self)。首先
searchController?.searchBar.text=filterOptions?.name
获取会议(使用:过滤器选项)
}
私人func会议(使用过滤选项:过滤选项?){
guard let options=filterOptions,let lat=options.latitude.value,let long=options.longitude.value else{return}
请求(.meetings(day:“Sun”,program:“AA”,region:“\(lat),\(long),10”){(result)in
切换结果{
成功案例(让我们回答):
做{
self.meetings=尝试响应.map([Meeting].self)
}捕捉错误{
打印(“错误解码会议:\(错误)”)
}
案例。失败(let错误):
打印(“获取会议时出错:\(错误)”)
}
}
}
}
//标记:-@objc函数
扩展会议可视控制器{
@objc func presentFilterModal(){
performsgue(标识符为“presentFilterModal”,发送方:nil)
}
@objc func presentMapModal(){
性能检查(标识符为“presentMapModal”,发送方:无)
}
}
//标记:-表视图数据源/委托
扩展会议视图控制器:UITableViewDataSource、UITableViewDelegate{
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回会议?计数??0
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(标识符为:“meetingCell”,表示:indexath)
cell.textlab?.text=“\(会议?[indexath.row].name???”)
cell.detailTextLabel?.text=“\(会议?[indexPath.row].day???”)
返回单元
}
}
//标记:-搜索栏代理
扩展会议可视控制器:UISearchBarDelegate{
func SearchBartextdidediting(searchBar:UISearchBar){
updateViews()
}
}
以下是我希望它看起来的样子:

以下是它目前的样子:


我知道我参加聚会有点晚了,但我遇到了同样的问题,我通过将
导航项.hidesearchbarwhenscrolling=false
添加到我的
setupSearchController
功能中解决了这个问题。 因此,在您的情况下,函数如下所示:

private func setupSearchController() {
    let storyboard = UIStoryboard(name: "Main", bundle: .main)
    let searchResultController = storyboard.instantiateViewController(withClass: SearchResultTableViewController.self)

    searchController = UISearchController(searchResultsController: searchResultController)
    searchController?.searchBar.placeholder = "neighborhood, city, state, or zip code"
    searchController?.searchBar.delegate = self
    searchController?.searchResultsUpdater = searchResultController
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false

}