奇怪的问题,我的UISearchController的空间 [英] Strange issue, space of my UISearchController
问题描述
问题是这样的:
搜索栏
和 navbar之间有空格
:
然后在 Debug in Hierarchy 中:
表视图
然后在故事板
中,将约束设置为 tableView
,注意: navigationBar
我的代码
导入UIKit
导入SVProgressHUD
类StoreListViewController:UIViewController,UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating {
@IBOutlet弱var导航栏: UINavigationBar!
@IBOutlet弱变量tableView:UITableView!
var ori_dataSource:[StoreListModel] = [StoreListModel]()
var dataSource = [String]()
var filterdDataSource = [String]()
var resultSearchController = UISearchController()
var selecteddStore:StoreListModel? = nil
覆盖func viewDidLoad(){
super.viewDidLoad()
//加载视图后进行任何其他设置。
initData()
initUI()
}
//标记:-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.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(){
// 1.隐藏单元下的行
tableView.separatorStyle = UITableViewCellSeparatorStyle.none
}
覆盖func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处置所有可以重新创建的资源。
}
//标记:-导航
//在基于情节提要的应用程序中,您通常会想做导航之前需要做一些准备
覆盖func prepare(for segue:UIStoryboardSegue,sender:Any?){
//使用segue.destinationViewController获取新的视图控制器。
//将选定的对象传递给新的视图控制器。
如果segue.identifier == StoreListGotoStoreDetailVC {
让detail_vc:StoreDetailVC = segue.destination为! StoreDetailVC
detail_vc.store_info = self.choosedStore
}
}
//标记:-委托
func searchBarCancelButtonClicked(){
用于item:self.tableView.constraints中的NSLayoutConstraint {
如果item.firstAttribute == NSLayoutAttribute.top {
项。常数= 0
}
}
}
//标记:-搜索栏委托
func searchBarTextDidBeginEditing(_ searchBar:UISearchBar){
searchBar.setValue(取消,forKey: _ cancelButtonText)
for item:NSLayoutConstraint在self.navbar.constraints {
如果item.firstAttribute == NSLayoutAttribute.height {
item.constant = 0
}
}
}
func searchBarCancelButtonClicked(_ searchBar:UISearchBar){
对于item:self.navbar.constraints中的NSLayoutConstraint {
如果item.firstAttribute == NSLayoutAttri bute.height {
item.constant = 64
}
}
}
//标记:-私有方法
//标记:-动作
//标记:-展开
@IBAction func unwindToStoreListVCFromStoreDetailVC(segue:UIStoryboardSegue){
}
//标记:-网络
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:{(结果)在
let stateCode = UtilSwift.getNetStateCode(结果:结果为Any,键:Global.net_key_stateCode)
如果stateCode == 0 {
let userArr:[[String:Any]] = UtilSwift.getNetAnyObject(结果:r结果为任何,键:列表)为! [[[String:Any]] // Global.net_key_bussiness后台写错了
//self.ori_dataSource = terantArr
for item:[String:任何] userArr {
让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
} stateCode == -1 {
//弹窗系统错误
SVProgressHUD.showError(withStatus:系统错误)
}
}, failBlcok:{
//弹窗系统错误
SVProgressHUD.showError(withStatus:网络异常)
})
}
//标记:-tableView
func tableView(_ tableView:UITableView,numberOfRowsInSection s部分:Int)-> Int {
如果self.resultSearchController.isActive {
return filterdDataSource.count
} else {
return dataSource.count
}
}
func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell {
让单元格:TerantListCell = tableView.dequeueReusableCell(withIdentifier: TerantListCell,for:indexPath)为! TerantListCell
//如果self.resultSearchController.isActive {
cell.title_label.text = self.filterdDataSource [indexPath.row]
配置单元
}其他{
cell.title_label?.text = self.dataSource [indexPath.row]
}
返回单元格
}
func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath){
self.choosedStore = self.ori_dataSource [indexPath.row]
tableView.deselectRow(在:indexPath,动画:true)
self.performSegue(withIdentifier: StoreListGotoStoreDetailVC,发送者:self)
}
// MARK :-正则
func updateSearchResults(for 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()
}
}
尝试-1
当我取消选中
调整滚动视图插图
时,首先进入 vc
,该问题不存在,但是如果单击 searchBar
,该问题将再次出现,我怀疑我的代码是否存在错误:
尝试-2
如果我在下面尝试此代码:
func searchBarCancelButtonClicked(_ searchBar:UISearchBar){
for item:NSLayoutConstraint在self.navbar.constraints {
如果item.firstAttribute == NSLayoutAttribute.height {
i tem.constant = 44 //之前是64。
}
}
}
经过多次尝试:
func searchBarCancelButtonClicked(_ searchBar:UISearchBar){
item:NSLayoutConstraint在self.navbar.constraints {
如果item.firstAttribute == NSLayoutAttribute.height {
item.constant = 64
}
}
//设置此功能对我有帮助! :)
tableView.contentInset = UIEdgeInsets.zero
tableView.scrollIndicatorInsets = UIEdgeInsets.zero
}
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() {
// 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()
}
}
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
}
这篇关于奇怪的问题,我的UISearchController的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!