iOS导航栏中的SearchBar在触摸时会稍微向下移动

时间:2018-08-18 16:47:05

标签: ios swift uisearchbar uisearchcontroller googleplacesautocomplete

我在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

when search bar first loads after tapping search bar

1 个答案:

答案 0 :(得分:0)

尝试:

    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
  

根据您的需要设置框架