如何通过 Swift 中的操作 show/hide UISearchBar?
How to show/hide UISearchBar by action in Swift?
我有一个 UITableView
和一个 UISearchBar
。我需要 show/hide 我的 searchBar
,通过用户操作(按下按钮):
我尝试使用此代码:
if self.tableView.contentOffset.y == 0 {
self.tableView.contentOffset = CGPoint(x: 0.0, y: self.searchBar.frame.size.height)
}
来自 this 问题。
实际上我已经尝试了所有这些答案。所有这些都只是滚动我的 UITableView
,每次我滚动我的 UITableView
- searchBar
都会出现。
我试着做这样的事情:
self.newsTableView.tableHeaderView = nil; //Hide
和
self.newsTableView.tableHeaderView = self.SearchBar; //Show
但是UITableView
不想return searchBar
;
我该如何解决这个问题?
我需要通过操作隐藏 searchBar
,而不是滚动我的 UITableView
(像 searchBar.hidden = true
一样隐藏)实际上,searchBar.hidden = true
有效,但是有一个白色的 space searchBar
.
希望这可以解决您的问题。在启动 tableview 时调用它来隐藏搜索栏,当你向下滚动时它会出现
var newBounds: CGRect? = self.newsTableView.bounds
newBounds?.origin.y = 0
newBounds?.origin.y = newBounds!.origin.y + self.searchBar.bounds.size.height
然后设置tableView的新边界
对 searchBar 和 tableView 使用 UIView.animation
当你开始滚动时 table
更改 position/alpha 搜索栏和 tableView
的高度
UIView.animateWithDuration(1.0, animations: {
searchBar.alpha = 0.0
tableView.view.frame.height = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height)
}, completion: {
(value: Bool) in
//do nothing after animation
})
在 Xcode 7.3 的 ViewController.swift 文件中,它对我来说工作正常
CategoryTableView.tableHeaderView = searchController.searchBar // show
CategoryTableView.tableHeaderView = nil // hide
searchController.active = false
我有一个 UITableView
和一个 UISearchBar
。我需要 show/hide 我的 searchBar
,通过用户操作(按下按钮):
我尝试使用此代码:
if self.tableView.contentOffset.y == 0 {
self.tableView.contentOffset = CGPoint(x: 0.0, y: self.searchBar.frame.size.height)
}
来自 this 问题。
实际上我已经尝试了所有这些答案。所有这些都只是滚动我的 UITableView
,每次我滚动我的 UITableView
- searchBar
都会出现。
我试着做这样的事情:
self.newsTableView.tableHeaderView = nil; //Hide
和
self.newsTableView.tableHeaderView = self.SearchBar; //Show
但是UITableView
不想return searchBar
;
我该如何解决这个问题?
我需要通过操作隐藏 searchBar
,而不是滚动我的 UITableView
(像 searchBar.hidden = true
一样隐藏)实际上,searchBar.hidden = true
有效,但是有一个白色的 space searchBar
.
希望这可以解决您的问题。在启动 tableview 时调用它来隐藏搜索栏,当你向下滚动时它会出现
var newBounds: CGRect? = self.newsTableView.bounds
newBounds?.origin.y = 0
newBounds?.origin.y = newBounds!.origin.y + self.searchBar.bounds.size.height
然后设置tableView的新边界
对 searchBar 和 tableView 使用 UIView.animation 当你开始滚动时 table 更改 position/alpha 搜索栏和 tableView
的高度UIView.animateWithDuration(1.0, animations: {
searchBar.alpha = 0.0
tableView.view.frame.height = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height)
}, completion: {
(value: Bool) in
//do nothing after animation
})
在 Xcode 7.3 的 ViewController.swift 文件中,它对我来说工作正常
CategoryTableView.tableHeaderView = searchController.searchBar // show
CategoryTableView.tableHeaderView = nil // hide
searchController.active = false