在取消按钮操作后删除搜索栏

时间:2017-09-28 08:46:46

标签: ios iphone swift searchbar

SearchBartableHeaderView。当用户搜索某些内容时,以下内容应隐藏SearchBar。我想使用以下代码行: searchController.hidesNavigationBarDuringPresentation = true

在我输入“取消”按钮操作后,搜索栏不会返回正确位置(请在下面找到附件截图)

到目前为止,这是我的代码:

@IBOutlet weak var tableView: UITableView!{
        didSet{
            tableView.estimatedRowHeight = 144
            tableView.rowHeight = UITableViewAutomaticDimension
            tableView.backgroundColor = UIColor.white
            tableView.makeAwareOfKeyboard()
            tableView.separatorStyle = .none
            tableView.reactive.delegate.forwardTo = self
            tableView.reactive.prefetchDataSource.forwardTo = self
            tableView.register(UINib(nibName: "UniversityTableViewCell", bundle: nil), forCellReuseIdentifier: UniversityTableViewCell.getIdentifier())
            tableView.register(UINib(nibName: "NoSearchDataTableViewCell", bundle: nil), forCellReuseIdentifier: NoSearchDataTableViewCell.getIdentifier())
        }
    }

override func viewDidLoad() {
    super.viewDidLoad()

    model2.result.bind(to: self) { me, t in
        guard let t = t  else  {return}
        me.callback(t.id,t.name)
    }

    let bond = UniversityBond(parent: self)
    model2.dsRead.bind(to: self.tableView, using: bond)

    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    //searchController.searchBar.barStyle = .default
    searchController.searchBar.isTranslucent = false
    searchController.searchBar.sizeToFit()
    searchController.hidesNavigationBarDuringPresentation = true
    searchController.searchBar.placeholder = "SEARCH_BAR_PLACEHOLDER_KEY".localized
    searchController.searchBar.backgroundImage = UIImage()
    searchController.searchBar.tintColor = UIColor(red: 50, green: 147, blue: 246)
    searchController.searchBar.barTintColor = UIColor(red: 215, green: 215, blue: 215)
    self.tableView.tableHeaderView = searchController.searchBar


    let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: UIColor(red: 50, green: 147, blue: 246)]
    UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], for: UIControlState.normal)

    definesPresentationContext = true
}


extension SelectUniversityViewController : UITableViewDelegate{

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        GAI.sharedInstance().sendView(name: AnalyticsView.selectUniversity)

        self.model2.selectedElement(indexPath)
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView()
        headerView.backgroundColor = UIColor.clear
        return headerView
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = UIView()
        footerView.backgroundColor = UIColor.clear
        return footerView
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
}



extension SelectUniversityViewController : UISearchResultsUpdating{

    func updateSearchResults(for searchController: UISearchController){

        guard let text = searchController.searchBar.text else {
            return
        }

        if text == self.model2.input.value {
            return
        }

        self.model2.input.value = searchController.searchBar.text!

        for task in tasks {
            task.cancel()
        }
        tasks.removeAll()
    }
}

感谢

enter image description here enter image description here

0 个答案:

没有答案