use*_*975 52 xcode uinavigationbar uisearchbar ios swift
所以我尝试了所有尝试将搜索栏放入Swift导航栏的内容.但遗憾的是,我还没有让它发挥作用,
对于那些不知道我在说什么的人,我正试图做这样的事情

请注意导航栏中的搜索栏.所以这就是我目前正在使用的内容
self.searchDisplayController?.displaysSearchBarInNavigationBar = true
Run Code Online (Sandbox Code Playgroud)
我把它弹出我的viewDidLoad,然后当我加载我提出的应用程序,只是一个空的导航栏.... :(任何想法?
Leo*_*Leo 69
试试这个
let leftNavBarButton = UIBarButtonItem(customView:Yoursearchbar)
self.navigationItem.leftBarButtonItem = leftNavBarButton
Run Code Online (Sandbox Code Playgroud)
更新
你保留了一个懒惰的UISearchBar属性
lazy var searchBar:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 200, 20))
Run Code Online (Sandbox Code Playgroud)
在viewDidLoad中
searchBar.placeholder = "Your placeholder"
var leftNavBarButton = UIBarButtonItem(customView:searchBar)
self.navigationItem.leftBarButtonItem = leftNavBarButton
Run Code Online (Sandbox Code Playgroud)
如果您想使用故事板,只需将搜索栏拖动为插座,然后使用插座搜索栏替换惰性属性
ant*_*014 52
// 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
Run Code Online (Sandbox Code Playgroud)
Doc*_*oca 23
Swift 5、XCode 11、Storyboard 方式,因此您可以通过故事板轻松添加所有搜索栏属性,并且视图控制器类中的代码更少。
1.) 在viewcontroller 中添加您的搜索栏视图作为外部视图。
2.) 将 searchBarView 连接到您的视图控制器。
3.) 将您的 searchBarView 添加到您的导航栏标题项。
navigationItem.titleView = searchBarView
Run Code Online (Sandbox Code Playgroud)
结果:
Mar*_*ger 19
在 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
}
Run Code Online (Sandbox Code Playgroud)
并且某些类应该符合 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()
}
}
Run Code Online (Sandbox Code Playgroud)
这将产生如下内容:
iAm*_*mcR 16
在您的视图控制器中:
lazy var searchBar = UISearchBar(frame: CGRectZero)
override func viewDidLoad() {
super.viewDidLoad()
searchBar.placeholder = "Search"
navigationItem.titleView = searchBar
}
Run Code Online (Sandbox Code Playgroud)
这样做,通过设置navigationItem.titleView,搜索栏自动在iPhone和iPad设备上居中.注意:仅使用v8.4和v9.0进行测试
对于SWIFT 3
lazy var searchBar = UISearchBar(frame: CGRect.zero)
Run Code Online (Sandbox Code Playgroud)
Hus*_*sam 11
navigationItem.searchController = searchController
Run Code Online (Sandbox Code Playgroud)
navigationItem.titleView = searchController.searchBar;
Run Code Online (Sandbox Code Playgroud)
或者您可以按照本答案中的描述将其分配为 leftBarButtonItem
| 归档时间: |
|
| 查看次数: |
59312 次 |
| 最近记录: |