在Swift的导航栏中获取搜索栏

时间:2014-11-04 01:57:12

标签: ios xcode swift uinavigationbar uisearchbar

所以我尝试了将搜索栏放到Swift导航栏中的所有内容。但遗憾的是我还没有让它发挥作用,只是......

对于那些不知道我在说什么的人,我正试图做这样的事情

enter image description here

请注意导航栏中的搜索栏。所以这就是我目前正在使用的

self.searchDisplayController?.displaysSearchBarInNavigationBar = true

我在viewDidLoad中弹出,然后当我加载我提供的应用时,只是一个空导航栏.... :(任何想法?

11 个答案:

答案 0 :(得分:65)

试试这个

var leftNavBarButton = UIBarButtonItem(customView:Yoursearchbar)
  self.navigationItem.leftBarButtonItem = leftNavBarButton

<强>更新

你保留了一个懒惰的UISearchBar属性

lazy   var searchBar:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 200, 20))

在viewDidLoad

searchBar.placeholder = "Your placeholder"
var leftNavBarButton = UIBarButtonItem(customView:searchBar)
self.navigationItem.leftBarButtonItem = leftNavBarButton

如果您想使用故事板 只需将搜索栏拖动为插座,然后用插座搜索栏替换惰性属性

答案 1 :(得分:47)

    // create the search bar programatically since you won't be
    // able to drag one onto the navigation bar
    searchBar = UISearchBar()
    searchBar.sizeToFit()

    // the UIViewController comes with a navigationItem property
    // this will automatically be initialized for you if when the
    // view controller is added to a navigation controller's stack
    // you just need to set the titleView to be the search bar
    navigationItem.titleView = searchBar

答案 2 :(得分:15)

在视图控制器中:

lazy var searchBar = UISearchBar(frame: CGRectZero)

override func viewDidLoad() {
    super.viewDidLoad()

    searchBar.placeholder = "Search"

    navigationItem.titleView = searchBar
}

这样做,通过设置navigationItem.titleView,搜索栏自动在iPhone和iPad设备上居中。注意:仅使用v8.4和v9.0进行测试

适用于SWIFT 3

lazy var searchBar = UISearchBar(frame: CGRect.zero)

答案 3 :(得分:10)

Swift 5,XCode 11,情节提要(Storyboard)方式,因此您可以轻松地通过 storyboard 添加所有搜索栏属性,并且视图控制器类中的代码更少。< / p>

1。)在Viewcontroller中将搜索栏视图添加为外部视图。

enter image description here

2。)将searchBarView连接到ViewController。

enter image description here

3。)将searchBarView添加到NavigationBar标题项中。

navigationItem.titleView = searchBarView

结果:

enter image description here

答案 4 :(得分:4)

在2019年,您应该使用UISearchController。

library(ggplot2)
library(ggmap)

lat <- c(47.5112, 47.7210, 47.3684)
long <- c(-122.257, -122.319, -122.031)
price <- c(287655, 456355, 662500)

House <- data.frame(long, lat, price)

House$MapColor <- ifelse(House$price >= 400000, "red", "blue")

wa_df <- map_data("state") %>%
  filter(region == "washington")

wa_county <- map_data("county") %>%
  filter(region == "washington")

ggplot(data = wa_df, mapping = aes(x = long, y = lat, group = group))+  
  geom_polygon(color = "black", fill = NA)+
  geom_polygon(data = wa_county, fill = NA, color = "black")+
  geom_point(data = House, mapping = aes(x = long, y = lat), color = House$MapColor)+
  coord_fixed(1.3)+
  theme_nothing()

并且某些类应符合UISearchResultsUpdating。我通常将此添加为我的ViewModel扩展。

override func viewDidLoad() {
    super.viewDidLoad()

    let searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self.viewModel
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search artists"
    self.navigationItem.searchController = searchController
    self.definesPresentationContext = true
}

这将产生如下内容:

search bar

答案 5 :(得分:3)

对于 Swift 5 或 letter

此外,您可以使用此代码。 完全以编程方式

import UIKit

class SearchTableViewController: UITableViewController {

private lazy var searchController: UISearchController = {
    let sc = UISearchController(searchResultsController: nil)
    sc.searchResultsUpdater = self
    sc.delegate = self
    sc.obscuresBackgroundDuringPresentation = false
    sc.searchBar.placeholder = "Enter A Compiny Name Or Symbole"
    sc.searchBar.autocapitalizationType = .allCharacters
    return sc
}()

override func viewDidLoad() {
    super.viewDidLoad()
    setupNavigationBar()
}

   private func setupNavigationBar() {
       navigationItem.searchController = searchController
   }

 }

// MARK: - UISearchResult Updating and UISearchControllerDelegate  Extension
  extension SearchTableViewController: UISearchResultsUpdating, UISearchControllerDelegate {
    func updateSearchResults(for searchController: UISearchController) {
    
    }
 }

After Clicking Search ?

After Clicking Cancel Button In Search ?

答案 6 :(得分:1)

http://xxx.xxx.com/projects

答案 7 :(得分:0)

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

    searchBar.endEditing(true)
    searchBar.text = nil
    print("## search btn clicked : \(searchBar.text ?? "")")
} 

答案 8 :(得分:0)

对于Swift 4和5。

将视图控制器嵌入导航控制器

let searchBar = UISearchBar()
self.navigationItem.titleView = seachBar
[Here is a screenshot.][1]
let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: 
cancelButton.tintColor = #colorLiteral(red: 0.9994240403, green: 0.9855536819, blue: 0, alpha: 

searchBar.tintColor = #colorLiteral(red: 0.9994240403, green: 0.9855536819, blue: 0, alpha: 
self.navigationItem.rightBarButtonItem = cancelButton

答案 9 :(得分:0)

将SearchBar设置为titleView,将NavigationBar的高度更改为56。要解决此问题,您可以将searchBar嵌入视图中并将其设置为titleView。

    var offset: CGFloat = 20

    // If VC is pushed, back button should be visible
    if navigationController?.navigationBar.backItem != nil {
        offset = 40
    }

    let customFrame = CGRect(x: 0, y: 0, width: view.frame.size.width - offset, height: 44.0)
    let searchBarContainer = UIView(frame: customFrame)
    searchBar = UISearchBar(frame: customFrame)
    searchBarContainer.addSubview(searchBar)
    navigationItem.titleView = searchBarContainer

答案 10 :(得分:0)

对于iOS 11及更高版本

navigationItem.searchController = searchController

enter image description here

对于iOS 10及更低版本

navigationItem.titleView = searchController.searchBar;

enter image description here

,也可以按照this answer

中的说明将其分配为leftBarButtonItem。