如何实现这样的搜索栏?

时间:2016-05-05 03:17:26

标签: ios xcode search uisearchbar searchbar

我想在我的收藏夹视图上方添加一个搜索栏,按下它时会显示视频。https://www.youtube.com/watch?v=mgS2Pzy5eJk。谢谢。

3 个答案:

答案 0 :(得分:2)

您可以直接使用iOS默认的UISearchDisplayController作为以下教程:

UISearchDisplayController Tutorial

阅读本教程并试一试..

希望它有所帮助...

答案 1 :(得分:0)

当用户点击searchBar:

  1. 删除或隐藏NavigationBar
  2. 相应地调整搜索栏的自动布局约束。
  3. 根据需要添加动画
  4. 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.
    }


}