我在Google Place AutoComplete的导航栏中放置了一个搜索栏。当它出现时,它会很好地显示,但是当您在其中轻按时,它会稍微向下移动并停留在该位置,即使您单击“取消”也是如此,直到将titleView设置为nil然后再次返回,然后重置。这是在我的viewDidLoad方法中调用的相关代码:
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
searchController?.searchBar.sizeToFit()
searchController?.hidesNavigationBarDuringPresentation = false
definesPresentationContext = true
navigationController?.navigationBar.isTranslucent = true
searchController?.hidesNavigationBarDuringPresentation = false
navigationItem.titleView = nil
然后在需要时我将其设置为:
navigationItem.titleView = searchController?.searchBar
最佳答案
尝试这个:
searchController.searchBar.frame = CGRect(center: CGPoint(x: 15,y: 10), size: CGSize(width: 30, height: 30))
let vu = UIView(frame: CGRect(center: .zero, size: CGSize(width: self.view.frame.width - 10, height: 40)))
vu.addSubview(searchController.searchBar)
vu.backgroundColor = UIColor.clear
navigationItem.titleView = vu
根据您的需要设置框架
关于ios - iOS导航栏中的搜索栏在触摸时会稍微向下移动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51910737/