我正在尝试为iOs8找到displaySearchBarInNavigationBar的替代品,有什么我可以使用的(在
swift中)?
我尝试过self.navigationItem.titleView = resultSearchController.searchBar,但它没有做任何事情.
解决方法
您可以在不使用UINavigationController的情况下将UISearchBar放在UINavigationBar中而不会出现问题,但是您只需要进行一些小改动,首先需要在UINavigationBar中为UINavigationItem定义一个@IBOutlet,但它的名称需要与navigationItem属性不同在所有UIViewController类中定义,请参阅以下代码:
class ViewController: UIViewController,UISearchControllerDelegate,UISearchResultsUpdating,UISearchBarDelegate { var searchController : UISearchController! @IBOutlet weak var navigationItemBar: UINavigationItem! override func viewDidLoad() { super.viewDidLoad() self.searchController = UISearchController(searchResultsController: nil) self.searchController.searchResultsUpdater = self self.searchController.delegate = self self.searchController.searchBar.delegate = self self.searchController.hidesNavigationBarDuringPresentation = false self.searchController.dimsBackgroundDuringPresentation = true self.navigationItemBar.titleView = searchController.searchBar self.definesPresentationContext = true } func updateSearchResultsForSearchController(searchController: UISearchController) { } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
然后你可以在你的模拟器中看到这样的东西:
我希望这对你有帮助.