2016-12-06 3 views
1

La question est comme ceci:question étrange, l'espace de mon UISearchController

Il existe un espace entre searchbar et navbar:

There has space between searchbar and navbar

Et dans le Debug in Hierarchy:

La table Afficher

enter image description here

The View wrapper, nous pouvons voir l'espace (20 pix)

The wrapper View

Et dans le storyboard, je mets la contrainte à la tableView, attention: le navigationBar est draged par moi-même , le natif est caché par moi.

And in the storyboard, I set the constraint to the tableView


Mon 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() { 

    // 1.配置 
    self.resultSearchController = UISearchController(searchResultsController: nil) 
    self.resultSearchController.searchResultsUpdater = self 
    self.resultSearchController.dimsBackgroundDuringPresentation = false 
    self.resultSearchController.searchBar.sizeToFit() 
    self.resultSearchController.searchBar.placeholder = "搜索" 
    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() { 
    // 1.隐藏cell下的Line 
    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("取消", 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 // 商户id 
    ] 

    // 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() // 刷新tableView 

     }else if stateCode == -1 { 

      // 弹窗系统错误 
      SVProgressHUD.showError(withStatus: "系统错误") 
     } 

    }, failBlcok: { 

      // 弹窗系统错误 
      SVProgressHUD.showError(withStatus: "网络异常") 
     }) 

    } 

    // 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 

     // 配置cell 
     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() 
    } 


} 

TENTATIVE - 1

Quand je uncheck le Adjust Scroll View Insets, premier arrivé dans le vc, la question est inexistante, mais si je clique sur le searchBar, la question reviendra, je soupçonne mon code si est quelque part erreur:

The issue exist


TENTATIVE - 2

Si ce code ci-dessous je tente:

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 

    for item:NSLayoutConstraint in self.navbar.constraints { 

     if item.firstAttribute == NSLayoutAttribute.height { 

      item.constant = 44 // before is 64. 
     } 
    } 

} 

The issue

+0

frère vous utilisez le contrôleur de navigation par défaut? –

+0

@Himanshu Moradiya, oui, mais le 'navigationBar' est dragué par moi-même.parce que je pense que je peux éditer commodément le 'navigationBar', le' navigationBar' par défaut est caché par moi. – aircraft

+0

avez-vous défini le cadre de votre tableview et de la barre de recherche lors du codage lors de la modification des modifications. ? –

Répondre

0

Après de nombreuses tentatives:

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 
} 
0

Comme je ne sais pas comment vous contraintes votre point de vue. Je suppose que le problème est dû à automaticallyAdjustsScrollViewInsets. Vous devriez automaticallyAdjustsScrollViewInsets = false de votre ViewController. Ou vous devez contraindre votre UITableView à View.Top au lieu de TopLayoutGuide.Bottom.

+0

J'ai testé décocher le 'AdjustsScrollViewInsets' dans le storyboard, mais il y a toujours le problème, vérifiez mon' EDIT - 1' – aircraft

+0

il serait bon de savoir comment vous contraindre votre barre de navigation. –

0
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 

    for item:NSLayoutConstraint in self.navbar.constraints { 

     if item.firstAttribute == NSLayoutAttribute.height { 

      item.constant = 44 // changes this one line and check it if any problem then put comment below. 
     } 
    } 
} 
+0

@aircraft votre tableview top constrain = 0; ? –

+0

@aircraft avez-vous mis à jour le fichier gif de votre résultat de sortie avec la valeur constante = 44 –

+0

. Vous pouvez voir mon 'Debug in Hierarchy:' l'image, là bas, la vue de dessous de tableView est OK, top to 'navigationBar' ' s en bas, mais la position 'searchbar' est décalée, ne commence pas par le sommet de' wrapperView'. – aircraft