简体   繁体   中英

SearchBar in iOS NavigationBar slightly moves down upon touch

I have a search bar I am placing inside of the navigation bar for Google Place AutoComplete. It shows up fine when it appears, but when you tap inside of it, it moves down slightly and stays there even if you hit cancel, until you set the titleView to nil and back again, then it resets. Here is the relevant code called in my viewDidLoad method:

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

Then when need I set it like so:

navigationItem.titleView = searchController?.searchBar

搜索栏首次加载时 点击搜索栏后

Try This:

    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

set frames according to your need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM