奇怪的问题,我UISearchController
问题的空间是这样的:奇怪的问题,我UISearchController
必须有searchbar
和navbar
之间的空间:
而在Debug in Hierarchy
:
该表查看
的包装查看,我们可以看到空间(20 PIX)
而在storyboard
,我的限制设置为tableView
,注意了:navigationBar
由自己draged ,本地人被我藏起来了。
我的代码
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()
}
}
尝试 - 1
当我uncheck
的,第一次来到这个vc
,这个问题是不存在的,但如果我点击searchBar
,这个问题会回来的,我怀疑我的代码,如果是某个错误:
尝试 - 2
如果我尝试下面这段代码:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
for item:NSLayoutConstraint in self.navbar.constraints {
if item.firstAttribute == NSLayoutAttribute.height {
item.constant = 44 // before is 64.
}
}
}
许多尝试后:
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
}
因为我不知道你限制你的意见如何。我想这个问题是由于automaticallyAdjustsScrollViewInsets
。您应该在ViewController
的automaticallyAdjustsScrollViewInsets = false
。或者您应该将您的UITableView
限制为View.Top
而不是TopLayoutGuide.Bottom
。
我已经测试了取消勾选故事板中的AdjustsScrollViewInsets,但是仍然存在问题,请检查我的编辑 - 1' – aircraft
知道如何限制导航栏会很棒。 –
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.
}
}
}
@aircraft your tableview top constrain = 0; ? –
@aircraft是否更新了输出结果gif文件,其常量= 44 –
。您可以看到我的'Debug in Hierarchy:'picture,at there,'tableView'的底部视图没问题,顶部到'navigationBar''但是'searchbar'的位置是偏移的,不是以'wrapperView'顶部开始的。 – aircraft
哥你使用默认的导航控制器? –
@Himanshu Moradiya,是的,但'navigationBar'是我自己制作的。因为我认为这样做可以方便地编辑'navigationBar',默认的'navigationBar'被我隐藏了。 – aircraft
是否在编辑更改时在编码中设置了tableview和searchbar的框架? ? –