我想在我的收藏夹视图上方添加一个搜索栏,按下它时会显示视频。https://www.youtube.com/watch?v=mgS2Pzy5eJk。谢谢。
答案 0 :(得分:2)
您可以直接使用iOS默认的UISearchDisplayController作为以下教程:
UISearchDisplayController Tutorial
阅读本教程并试一试..
希望它有所帮助...
答案 1 :(得分:0)
当用户点击searchBar:
时在func searchBarTextShouldBeginEditing(searchBar: UISearchBar)
func searchBarTextShouldBeginEditing(searchBar:UISearchBar){
//remove or hide the NavigationBar
//update auto layout constraint
UIView.animateWithduration(0.3) {
self.view.layoutIfNeeded()
}
return true
}
答案 2 :(得分:0)
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating {
@IBOutlet weak var tableview: UITableView!
let unfilteredNFLTeams = ["Bengals", "Ravens", "Browns", "Steelers", "Bears", "Lions", "Packers", "Vikings",
"Texans", "Colts", "Jaguars", "Titans", "Falcons", "Panthers", "Saints", "Buccaneers",
"Bills", "Dolphins", "Patriots", "Jets", "Cowboys", "Giants", "Eagles", "Redskins",
"Broncos", "Chiefs", "Raiders", "Chargers", "Cardinals", "Rams", "49ers", "Seahawks"].sorted()
var filteredNFLTeams: [String]?
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
filteredNFLTeams = unfilteredNFLTeams
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
tableview.tableHeaderView = searchController.searchBar
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let nflTeams = filteredNFLTeams else {
return 0
}
return nflTeams.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "nandhu", for: indexPath)
if let nflTeams = filteredNFLTeams {
let team = nflTeams[indexPath.row]
cell.textLabel!.text = team
}
return cell
}
func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text, !searchText.isEmpty {
filteredNFLTeams = unfilteredNFLTeams.filter { team in
return team.lowercased().contains(searchText.lowercased())
}
} else {
filteredNFLTeams = unfilteredNFLTeams
}
tableview.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}