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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26726520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:29:22  来源:igfitidea点击:

Get search bar in navigation bar in Swift

iosxcodeswiftuinavigationbaruisearchbar

提问by user302975

So I've tried everything trying to get a search bar into the navigation bar in Swift. But sadly I haven't gotten it working, just yet...

因此,我尝试了所有尝试将搜索栏放入 Swift 导航栏中的方法。但遗憾的是我还没有让它工作,只是......

For those of you who don't know what I'm talking about, I'm trying to do something like this

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

enter image description here

在此处输入图片说明

Note the search bar in the navigation bar. So here's what I'm currently using

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

self.searchDisplayController?.displaysSearchBarInNavigationBar = true

I popped that in my viewDidLoad, and then when I load up the app I'm presented with, just an empty navigation bar.... :( Any ideas?

我在我的viewDidLoad.

回答by Leo

Try this

尝试这个

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

Update

更新

You keep a lazy UISearchBar property

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

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

In viewDidLoad

在视图中加载

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

If you want to use storyboard just drag your searchbar as a outlet,then replace the lazy property with your outlet searchbar

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

回答by antonio081014

    // 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

回答by iAmcR

In your view controller:

在您的视图控制器中:

lazy var searchBar = UISearchBar(frame: CGRectZero)

override func viewDidLoad() {
    super.viewDidLoad()

    searchBar.placeholder = "Search"

    navigationItem.titleView = searchBar
}

Doing it this way, by setting the navigationItem.titleView, the search bar is automatically centered across the iPhone and iPad devices. Note: only tested with v8.4 and v9.0

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

for SWIFT 3

用于 SWIFT 3

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

回答by Martin Berger

In 2019, you should use UISearchController.

在 2019 年,您应该使用 UISearchController。

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
}

And some class should conform to UISearchResultsUpdating. I usually add this as extension to my ViewModel.

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

extension ArtistSearchViewModel: UISearchResultsUpdating {

func updateSearchResults(for searchController: UISearchController) {
    print("Searching with: " + (searchController.searchBar.text ?? ""))
    let searchText = (searchController.searchBar.text ?? "")
    self.currentSearchText = searchText
    search()
    }
}

This will spawn something like this:

这将产生如下内容:

search bar

搜索栏

回答by Husam

For iOS 11 and above

适用于 iOS 11 及更高版本

navigationItem.searchController = searchController

enter image description here

在此处输入图片说明

For iOS 10 and below

对于 iOS 10 及以下

navigationItem.titleView = searchController.searchBar;

enter image description here

在此处输入图片说明

or you can assign it as leftBarButtonItem as described in this answer

或者您可以按照本答案中的描述将其分配为 leftBarButtonItem

回答by luhuiya

    let searchBar = UISearchBar()
    searchBar.sizeToFit()
    searchBar.placeholder = ""
    self.navigationController?.navigationBar.topItem?.titleView = searchBar

回答by Arijan

Setting SearchBar as titleView, changes height of navigationBar to 56. To fix this, you can embed searchBar in view and set that as titleView.

将 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

回答by Sami Yousif

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

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

回答by Manas Sharma

For Swift 4 and 5.

对于 Swift 4 和 5。

Embed your View Controller in a Navigation Controller

将您的视图控制器嵌入到导航控制器中

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