ios - 用户取消搜索时约束不变

标签 ios swift

所以我有一个搜索图标作为我的右栏按钮项。当用户点击该图标时,它允许用户搜索并仅在表格 View 中显示某些值。它还隐藏了顶部的导航栏按钮和导航 Controller 正下方的 filterBar

func setupNavBarButtons() {
    let searchImage = UIImage(named: "search_icon")?.withRenderingMode(.alwaysOriginal)
    let searchBarButtonItem = UIBarButtonItem(image: searchImage, style: .plain, target: self, action: #selector(handleSearch))
    navigationItem.rightBarButtonItem = searchBarButtonItem
    setupFilterButton()
}

像这样在搜索时过滤要隐藏的栏和导航栏项目:

func handleSearch() {
    searchController.searchBar.isHidden = false
    navigationItem.titleView = searchController.searchBar
    searchController.searchBar.becomeFirstResponder()
    navigationItem.rightBarButtonItems = nil
    navigationItem.leftBarButtonItems = nil
    filterBar.isHidden = true
    tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
}

然后它会在用户停止搜索后再次出现,如下所示:

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    setupNavBarButtons()
    searchController.searchBar.isHidden = true
    filterBar.isHidden = false
    tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 40).isActive = true
    // Also tried tableView.topAnchor.constraint(equalTo: filterBar.bottomAnchor).isActive = true
}

搜索前:

enter image description here

搜索期间

enter image description here

搜索后:如您所见,tableview 没有返回到原来的位置。 filterBar 是带有“ map ”和“位置”的灰色 View

enter image description here

仍然有同样的问题,所以我在这里上传了我的项目:

https://github.com/lukejones1/bug

最佳答案

首先,您添加了 40 像素的“高度”布局约束。 当用户单击搜索按钮时,您添加了 0 px 的“高度”布局约束。 再次,当用户单击取消按钮时,您添加了 40 像素的“高度”布局约束。

您需要重用布局约束。

class ViewController: UIViewController {

var filterBarHeightLC : NSLayoutConstraint?

lazy var tableView : UITableView = {
    let tv = UITableView()
    tv.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
    tv.layoutMargins = UIEdgeInsets.zero
    tv.separatorInset = UIEdgeInsets.zero
    tv.backgroundColor = .red
    tv.translatesAutoresizingMaskIntoConstraints = false
    return tv
}()

lazy var filterBar : UIView = {
    let bar = UIView()
    bar.backgroundColor = .blue
    bar.translatesAutoresizingMaskIntoConstraints = false
    return bar
}()

fileprivate lazy var filterButton : UIButton = {
    let button = UIButton()
    button.setTitleColor(UIColor.white, for: UIControlState())
    button.setTitle("Filter", for: UIControlState())
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()

fileprivate lazy var searchController: UISearchController = {
    let sc = UISearchController(searchResultsController:  nil)
    sc.dimsBackgroundDuringPresentation = false
    sc.hidesNavigationBarDuringPresentation = false
    sc.searchResultsUpdater = self
    sc.delegate = self
    sc.view.tintColor = UIColor.white
    sc.searchBar.tintColor = UIColor.white
    sc.searchBar.delegate = self
    return sc
}()

func setupNavBarButtons() {
    let searchBarButtonItem = UIBarButtonItem(title: "Search", style: .plain, target: self, action: #selector(handleSearch))
    navigationItem.rightBarButtonItem = searchBarButtonItem
    setupFilterButton()
}

func setupFilterButton() {
    let containerView = UIView()
    containerView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
    containerView.addSubview(filterButton)
    filterButton.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
    filterButton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
    let filterBarButtonItem = UIBarButtonItem(customView: containerView)
    navigationItem.leftBarButtonItem = filterBarButtonItem
}

override func viewDidLoad() {
    super.viewDidLoad()
    setupViews()
    setupNavBarButtons()
    view.backgroundColor = .white
}

func handleSearch() {
    searchController.searchBar.isHidden = false
    navigationItem.titleView = searchController.searchBar
    searchController.searchBar.becomeFirstResponder()
    navigationItem.rightBarButtonItems = nil
    navigationItem.leftBarButtonItems = nil

    // changed!
    filterBarHeightLC?.constant = 0
}

func setupViews() {
    view.addSubview(filterBar)
    view.addSubview(tableView)

    tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    tableView.topAnchor.constraint(equalTo: filterBar.bottomAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

    filterBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    filterBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    filterBar.topAnchor.constraint(equalTo: view.topAnchor, constant: 64).isActive = true

    // changed!
    filterBarHeightLC = filterBar.heightAnchor.constraint(equalToConstant: 40)
    filterBarHeightLC?.isActive = true
}

extension ViewController: UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {

func filteredContentForSearchText(_ searchText: String, scope: String = "All") {
    tableView.reloadData()
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    setupNavBarButtons()
    searchController.searchBar.isHidden = true

    // changed!
    filterBarHeightLC?.constant = 40
}

func updateSearchResults(for searchController: UISearchController) {
    filteredContentForSearchText(searchController.searchBar.text!)
}

祝您编码愉快! :)

关于ios - 用户取消搜索时约束不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39892955/

相关文章:

ios - 带有 UILabel 内容的 UITableViewCell 在滚动时发生变化

ios - UILocalNotification 无法正常工作

ios - iOS 11.2.6 更新后应用程序崩溃

ios - swift 3 [字符串 : AnyObject]() gives Cannot call value of non-function type UInt -> Data

swift - iOS13 UIAlertController 具有自定义 View 和首选样式作为操作表灰度所有颜色

swift - 由于滚动刷新时空 Collection View 崩溃

ios - 纯代码关于UICollectionView水平分页

ios - 需要有关 NSString 的帮助

ios - 每周询问用户费率

ios - 通过辅助方法呈现 View Controller