SearchController问题,搜索displayController时会显示搜索栏的间距 [英] SearchController issue, when search the displayController shows a spacing from the Searchbar
问题描述
问题是:
在故事板
中,我必须取消选中调整滚动视图插入
,因为如果不这样做,我会得到另一个问题(
我的代码
import UIKit
import SVProgressHUD
class ChooseStoreViewController:UIViewController,UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating {
@IBOutlet weak var tableView:UITableView!
var ori_dataSource:[StoreListModel] = [StoreListModel]()
var dataSource = [String]()
var filterdDataSource = [String]()
var resultSearchController = UISearchController()
var choosedStore:StoreListModel? = nil
var userInfoFromChooseTerant:[String:Any]?
@IBOutlet weak var top_constraint:NSLayoutConstraint!
覆盖func viewDidLoad(){
super.viewDidLoad()
initData()
initUI()
}
// MARK: - 查看生活
覆盖func viewWillAppear(_ animated:Bool){
super.viewWillAppear(animated)
self.navigationController ?。isNavigationBarHidden = false
}
覆盖func viewWillDisappear(_ animated:Bool){
super.viewWillDisappear(animated)
self.navigationController?.isNavigationBarHidden = true
}
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)
//如果要使用我们的子类,则必需:dequeueReusableCellWithIdentifier:forIndexPath:
//tableView.register(nib ,forCellReuseIdentifier:TerantListCell)
self.tableView.register(nib,forCellReuseIdentifier:TerantListCell)
self.tableView.tableFooterView = UIView.init()
self。 tableView.reloadData()
networkForStoreList()
}
func initUI(){
let backNavItem:UIBarButtonItem = UtilSwift.addBackButtonItem(nil,controlelr :self)
backNavItem.action = #selector(navBack)
// print(userInfoFromChooseTerant!)
// 2.隐藏单元格的行
tableView.separatorStyle = UITableViewCellSeparatorStyle.none
// 3.tableView
}
// MARK: - 导航
//在基于故事板的应用程序中,您经常需要在导航之前做一些准备
覆盖func prepare(对于segue:UIStoryboardSegue,发送者:任何?){
//使用segue.destinationViewController获取新的视图控制器。
//将所选对象传递给新视图控制器。
让chooseRole:ChooseRoleViewController = segue.destination为! ChooseRoleViewController
chooseRole.userInfoFromChooseStore = self.userInfoFromChooseTerant
}
// MARK: - 搜索委托
func searchBarCancelButtonClicked(){$项目的b
$ b:self.tableView.constraints中的NSLayoutConstraint {
self.view.setNeedsLayout()
if item.firstAttribute == NSLayoutAttribute.top {
item.constant = 0
}
}
}
// MARK: - 搜索栏委托
func searchBarTextDidBeginEditing(_ searchBar:UISearchBar){
searchBar.setValue(取消,forKey:_ cancelButtonText)
}
func searchBarCancelButtonClicked(_ searchBar:UISearchBar){
}
// MARK: - 私有方法
func navBack(){
_ = self.navigationController?.popViewController (动画:true)
}
// MARK: - acti ons
@IBAction func unwindToChooseStoreVCFromChooseRole(segue:UIStoryboardSegue){
}
@IBAction func nextStepAction(_ sender:UIButton) {
//可以不选择门店,非必选
/ *如果choosedStore == nil {
让lml_alert:LMLDropdownAlertView = LMLDropdownAlertView.init(框架: self.view.bounds)
lml_alert.showAlert(标题:全球。鹿鸣提示标题,detail_Title:请选择门店!,cancleButtonTitle:取消,confirmButtonTitle:确定,动作:{(按钮) in
})
返回
} * /
self.resultSearchController.isActive = false //失活
if self.choosedStore!= nil {
_ = self.userInfoFromChooseTerant?.updateValue(self.choosedStore!.userId,forKey:store_id)
}
self.performSegue(withIdentifier:ChooseStoreVCToChooseRoleVC,发件人:self)
}
// MARK: - 网络
func networkForStoreList(){
让params:[String:String] = [
createTime: - 1,
userId:self.userInfoFromChooseTerant![affiliated_id] as! String
]
// url_terantList
Mysevers.afpost(withHud:true,andAddressname:Global.url_listStore,parmas:params,requestSuccess:{(result)in
让stateCode = UtilSwift.getNetStateCode(结果:结果为Any,key:Global.net_key_stateCode)
if stateCode == 0 {
let storeArr:[[String:Any]] = UtilSwift.getNetAnyObject(结果:结果为Any,key:list)as![[String:Any]] // Global.net_key_bussiness后台写错了
//项目的self.ori_dataSource = terantArr
:storeArr中的[String:Any] {
let store_list_model:StoreListModel = StoreListModel.initStoreListModelWithDic(dic:item)
self.ori_dataSource.append (store_list_model)
}
项目:StoreListModel in self.ori_dataSource {
self.dataSource.appen d(item.name)
}
self.tableView.reloadData()//刷新tableView
}否则如果是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 {
让cell:TerantListCell = tableView.dequeueReusableCell(withIdentifier:TerantListCell,for:indexPath)as! TerantListCell
//配置单元
如果self.resultSearchController.isActive {
cell.title_label.text = self.filterdDataSource [indexPath.row]
} else {
cell.title_label?.text = self.dataSource [indexPath.row]
}
返回单元格
}
func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath){
self.choosedStore = self.ori_dataSource [indexPath.row]
}
// MARK: - regexp
func updateSearchResults(对于searchController:UISearchController){
self.filterdDataSource.removeAll(keepingCapacity:false)
让searchPredicate = NSPredicate(格式:SELF CONTAINS [c]%@,searchController.searchBar.text!)
let array =(self.dataSource as NSArray) .filtered(using:searchPredicate)
self.filterdDataSource = array as! [String]
self.tableView.reloadData()
}
}
转到存在ChooseStoreViewController的.storyboard文件。然后单击UITableView并更改tableView约束,如下所示:
检查顶部空间约束。
The issue is this:
In the storyboard
, I must uncheck the Adjust Scroll View Insets
, because if not do this, I will get a other issue(UISearchController issue, NSLayoutAttribute do not work in real device), and I don't know this if is affect the issue here.(I test in simulator, if check Adjust Scroll View Insets
, the issue here will not appear )
My code
import UIKit
import SVProgressHUD
class ChooseStoreViewController: UIViewController,UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {
@IBOutlet weak var tableView: UITableView!
var ori_dataSource: [StoreListModel] = [StoreListModel]()
var dataSource = [String]()
var filterdDataSource = [String]()
var resultSearchController = UISearchController()
var choosedStore:StoreListModel? = nil
var userInfoFromChooseTerant:[String:Any]?
@IBOutlet weak var top_constraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
initData()
initUI()
}
// MARK: - view life
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.isNavigationBarHidden = true
}
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() {
let backNavItem:UIBarButtonItem = UtilSwift.addBackButtonItem(nil, controlelr: self)
backNavItem.action = #selector(navBack)
// print(userInfoFromChooseTerant!)
// 2.隐藏cell下的Line
tableView.separatorStyle = UITableViewCellSeparatorStyle.none
// 3.tableView
}
// 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.
let chooseRole: ChooseRoleViewController = segue.destination as! ChooseRoleViewController
chooseRole.userInfoFromChooseStore = self.userInfoFromChooseTerant
}
// MARK: - search delegate
func searchBarCancelButtonClicked() {
for item:NSLayoutConstraint in self.tableView.constraints {
self.view.setNeedsLayout()
if item.firstAttribute == NSLayoutAttribute.top {
item.constant = 0
}
}
}
// MARK: - searchbar delegate
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.setValue("取消", forKey:"_cancelButtonText")
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
}
// MARK: - private methods
func navBack() {
_ = self.navigationController?.popViewController(animated: true)
}
// MARK: - actions
@IBAction func unwindToChooseStoreVCFromChooseRole(segue: UIStoryboardSegue){
}
@IBAction func nextStepAction(_ sender: UIButton) {
// 可以不选择门店,非必选
/*if choosedStore == nil {
let lml_alert: LMLDropdownAlertView = LMLDropdownAlertView.init(frame: self.view.bounds)
lml_alert.showAlert(title: Global.鹿鸣提示标题, detail_Title: "请选择门店!", cancleButtonTitle: "取消", confirmButtonTitle: "确定", action: { (button) in
})
return
}*/
self.resultSearchController.isActive = false // 失活
if self.choosedStore != nil {
_ = self.userInfoFromChooseTerant?.updateValue(self.choosedStore!.userId, forKey: "store_id")
}
self.performSegue(withIdentifier: "ChooseStoreVCToChooseRoleVC", sender: self)
}
// MARK: - network
func networkForStoreList() {
let params:[String:String] = [
"createTime":"-1",
"userId" : self.userInfoFromChooseTerant!["affiliated_id"] as! String
]
// url_terantList
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 storeArr:[[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 storeArr {
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]
}
// MARK: - regexp
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()
}
}
Go to ".storyboard" file where "ChooseStoreViewController" exist. Then click on UITableView and change tableView constraints as follows:
Check Top Space constraint.
这篇关于SearchController问题,搜索displayController时会显示搜索栏的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!