我有一个显示不同子视图的视图控制器。每个子视图都是通过视图控制器调用var items = new List<(int id, string name)>();
方法创建的,并且视图控制器将自身作为参数传递给该视图。
UINib()
一旦视图收到对其父视图控制器的引用,它将设置适当的导航栏
class func instanceFromNib(parentViewController: UIViewController) -> UIView {
let view = UINib(nibName: "PodcastsPage", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! PodcastsPage
view.viewController = parentViewController
return view as UIView
}
在private var viewController: UIViewController! {
didSet {
setupNavigationBar()
}
}
内,我有一些代码应将setupNavigationBar()
添加到视图控制器的导航栏中
UISearchController
虽然我运行应用程序时func setupNavigationBar() {
guard let navigationBar = viewController.navigationController?.navigationBar else {
return
}
navigationBar.isTranslucent = false
navigationBar.barTintColor = .navigationBar
navigationBar.titleTextAttributes = [
.font: UIFont(name: "AvenirNext-DemiBold", size: 18.0)!,
.foregroundColor: UIColor.textPrimary
]
viewController.navigationItem.title = "Follow Podcasts"
let nextButton = UIBarButtonItem(title: "Finish", style: .done, target: nil, action: nil)
nextButton.tintColor = .orangePrimary
viewController.navigationItem.setRightBarButton(nextButton, animated: false)
// Set UISearchController here //
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.placeholder = "Search"
searchController.searchResultsUpdater = self
viewController.navigationItem.searchController = searchController
viewController.navigationItem.hidesSearchBarWhenScrolling = false
}
不可见
但是,如果我像这样将UISearchController
设置在视图控制器的UISearchController
内,则实际上存在搜索栏。
viewDidLoad
要从子视图设置override func viewDidLoad() {
super.viewDidLoad()
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.placeholder = "Search"
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
}
,我该怎么做?