UISearchController search bar overlap first cell when active

前端 未结 7 2488
慢半拍i
慢半拍i 2020-12-21 08:52

I am using UISearchController to search for data in my tableview. I am not using table view controller. I would like to hide the navigation bar when my search b

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-21 09:46

    I have the same trouble. And research didn't answer. My decision:

    1) I used UITableviewController with SearchController and when I taped on the field I had this UI trouble: Extra blank space between searchBar and tableview The guys noted that you need to use the searchController with UIViewcontroller and tableView separately. So I did

    2) There was this problem. It is solved as follows:

     let searchController = UISearchController(searchResultsController: nil)
    var tableView:UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        loadData()
        setupTableView()
        setupSearchController()
    }
    
    private func setupTableView(){
    
        tableView = UITableView(frame: CGRect.zero, style: .plain)
    
        **//next 2 very important line of code**
    
        tableView.autoresizingMask = UIViewAutoresizing()
        tableView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(tableView)
    
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(LetterBrandCell.self, forCellReuseIdentifier: "brandCell")
        tableView.tableFooterView = UIView()
        addConstraints()
    }
    

    In the method of adding constraints, it is important to be attached to top & bottomLayoutGuides, not just to view:

    private func addConstraints(){
    
        NSLayoutConstraint(item: tableView,
                           attribute: .top,
                           relatedBy: .equal,
                           toItem: topLayoutGuide,
                           attribute: .bottom,
                           multiplier: 1,
                           constant: 0).isActive = true
    
        NSLayoutConstraint(item: tableView,
                           attribute: .bottom,
                           relatedBy: .equal,
                           toItem: bottomLayoutGuide,
                           attribute: .top,
                           multiplier: 1,
                           constant: 0).isActive = true
        NSLayoutConstraint(item: view,
                           attribute: .leading,
                           relatedBy: .equal,
                           toItem: tableView,
                           attribute: .leading,
                           multiplier: 1,
                           constant: 0).isActive = true
        NSLayoutConstraint(item: view,
                           attribute: .trailing,
                           relatedBy: .equal,
                           toItem: tableView,
                           attribute: .trailing,
                           multiplier: 1,
                           constant: 0).isActive = true
    
    }
    

提交回复
热议问题