You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何固定搜索栏位置?MapKit搜索开发遇导航栏按钮消失问题

Fixing Disappearing Plus Button When Using MapKit Search Bar in Navigation Title

I've run into this exact issue before—when you set the searchBar as the navigation title view, it can sometimes push out or hide existing bar button items if not configured properly. Let's fix this while keeping your search functionality intact.

The Root Cause

When you assign the search bar to navigationItem.titleView, it replaces the default title space, but it shouldn't automatically remove your right bar button item. The problem might be that your plus button wasn't explicitly retained or re-assigned after setting up the search controller, or there's a subtle conflict with presentation context settings.

Modified Working Code

Here's how to adjust your code to keep both the search bar and plus button visible and functional:

// First, explicitly define and set your plus button (add this if you haven't already)
let plusButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(plusButtonTapped))
navigationItem.rightBarButtonItem = plusButton

// Configure your search controller and search bar
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for places"

// Assign the search bar to the title view—this won't interfere with the right bar button item
navigationItem.titleView = resultSearchController?.searchBar

// Keep these presentation settings to maintain navigation bar visibility
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true

// Link your search table to the map and delegate
locationSearchTable.mapView = mapView
locationSearchTable.handleMapSearchDelegate = self

// Add the action handler for your plus button
@objc func plusButtonTapped() {
    // Insert your plus button functionality here
}

Key Fixes Explained

  • Explicitly Retain the Right Bar Button Item: By defining and assigning the plus button explicitly (either before or after setting the title view), you ensure it stays anchored in the navigation bar. The title view only occupies the center space, so right/left bar buttons should coexist without issue.
  • Lock Navigation Bar During Search: hidesNavigationBarDuringPresentation = false guarantees the navigation bar (and your plus button) stays visible even when search results are displayed.
  • Contain Presentation Context: definesPresentationContext = true ensures the search controller is presented within your view controller's context, preventing it from taking over the entire screen and hiding navigation elements.

If your plus button was already defined elsewhere, double-check that it's not being set to nil inadvertently after configuring the search bar. This setup should resolve the disappearance issue while keeping your MapKit search functionality working as expected.

内容的提问来源于stack exchange,提问作者AnotherCoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:42:35