The issue is like this:
There has space between searchbar
and navbar
:
And in the Debug in Hierarchy
:
The table View
The wrapper View, we can see the space (20 pix)
And in the storyboard
, I set the constraint to the tableView
, attention: the navigationBar
is draged by myself, the native is hide by me.
My Code
import UIKit
import SVProgressHUD
class StoreListViewController: UIViewController, UISearchBarDelegate, UITableViewDelegate,UITableViewDataSource, UISearchResultsUpdating {
@IBOutlet weak var navbar: UINavigationBar!
@IBOutlet weak var tableView: UITableView!
var ori_dataSource: [StoreListModel] = [StoreListModel]()
var dataSource = [String]()
var filterdDataSource = [String]()
var resultSearchController = UISearchController()
var choosedStore:StoreListModel? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
initData()
initUI()
}
// MARK: - init
func initData() {
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.sizeToFit()
self.resultSearchController.searchBar.placeholder = "search"
self.resultSearchController.searchBar.tintColor = UIColor.black
self.resultSearchController.searchBar.delegate = self
self.tableView.tableHeaderView = self.resultSearchController.searchBar
let nib = UINib(nibName: "TerantListCell", bundle: nil)
// Required if our subclasses are to use: dequeueReusableCellWithIdentifier:forIndexPath:
//tableView.register(nib, forCellReuseIdentifier: "TerantListCell")
self.tableView.register(nib, forCellReuseIdentifier: "TerantListCell")
self.tableView.tableFooterView = UIView.init()
self.tableView.reloadData()
networkForStoreList()
}
func initUI() {
tableView.separatorStyle = UITableViewCellSeparatorStyle.none
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "StoreListGotoStoreDetailVC" {
let detail_vc:StoreDetailVC = segue.destination as! StoreDetailVC
detail_vc.store_info = self.choosedStore
}
}
// MARK: - delegate
func searchBarCancelButtonClicked() {
for item:NSLayoutConstraint in self.tableView.constraints {
if item.firstAttribute == NSLayoutAttribute.top {
item.constant = 0
}
}
}
// MARK: - searchbar delegate
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.setValue("cancel", forKey:"_cancelButtonText")
for item:NSLayoutConstraint in self.navbar.constraints {
if item.firstAttribute == NSLayoutAttribute.height {
item.constant = 0
}
}
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
for item:NSLayoutConstraint in self.navbar.constraints {
if item.firstAttribute == NSLayoutAttribute.height {
item.constant = 64
}
}
}
// MARK: - private methods
// MARK: - actions
// MARK: - unwind
@IBAction func unwindToStoreListVCFromStoreDetailVC(segue: UIStoryboardSegue) {
}
// MARK: - network
func networkForStoreList() {
let userStatic: UserStaticSwift = UserStaticSwift.sharedInstance()
let params:[String:String] = [
"createTime":"-1",
"userId" : userStatic.userId
]
// url_listUsers
Mysevers.afpost(withHud: true, andAddressname: Global.url_listStore, parmas: params, requestSuccess: { (result) in
let stateCode = UtilSwift.getNetStateCode(result: result as Any, key: Global.net_key_stateCode)
if stateCode == 0 {
let userArr:[[String : Any]] = UtilSwift.getNetAnyObject(result: result as Any, key: "list") as! [[String : Any]] // Global.net_key_bussiness
//self.ori_dataSource = terantArr
for item:[String: Any] in userArr {
let store_list_model: StoreListModel = StoreListModel.initStoreListModelWithDic(dic: item)
self.ori_dataSource.append(store_list_model)
}
for item:StoreListModel in self.ori_dataSource {
self.dataSource.append(item.name)
}
self.tableView.reloadData()
}else if stateCode == -1 {
SVProgressHUD.showError(withStatus: "err")
}
}, failBlcok: {
SVProgressHUD.showError(withStatus: "fail")
})
}
// MARK: - tableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.resultSearchController.isActive {
return filterdDataSource.count
}else {
return dataSource.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: TerantListCell = tableView.dequeueReusableCell(withIdentifier: "TerantListCell", for: indexPath) as! TerantListCell
if self.resultSearchController.isActive {
cell.title_label.text = self.filterdDataSource[indexPath.row]
}else {
cell.title_label?.text = self.dataSource[indexPath.row]
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.choosedStore = self.ori_dataSource[indexPath.row]
tableView.deselectRow(at: indexPath, animated: true)
self.performSegue(withIdentifier: "StoreListGotoStoreDetailVC", sender: self)
}
// MARK: -
func updateSearchResults(for searchController: UISearchController) {
self.filterdDataSource.removeAll(keepingCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (self.dataSource as NSArray).filtered(using: searchPredicate)
self.filterdDataSource = array as! [String]
self.tableView.reloadData()
}
}
ATTEMPT - 1
When I uncheck
the Adjust Scroll View Insets
, first come into the vc
, the issue is nonexistent, but if I click the searchBar
, the issue will come again, I suspect my code if is somewhere mistake:
ATTEMPT - 2
If I try this code below:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
for item:NSLayoutConstraint in self.navbar.constraints {
if item.firstAttribute == NSLayoutAttribute.height {
item.constant = 44 // before is 64.
}
}
}
After many attempt:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
for item:NSLayoutConstraint in self.navbar.constraints {
if item.firstAttribute == NSLayoutAttribute.height {
item.constant = 64
}
}
// set this help me! :)
tableView.contentInset = UIEdgeInsets.zero
tableView.scrollIndicatorInsets = UIEdgeInsets.zero
}