美文网首页
searchController 使用出现的懵逼bug

searchController 使用出现的懵逼bug

作者: 李必琪 | 来源:发表于2016-12-09 14:41 被阅读0次

今天在做搜索功能的时候,用到了searchController
参考了
http://blog.csdn.net/longshihua/article/details/53101094
http://www.jianshu.com/p/0b3e18f58b33
https://github.com/codepath/ios_guides/wiki/Search-Bar-Guide#example-searching-a-collection-view-2

Bug
在点击搜索后,再点击取消之后,会出现下图问题,搜索条向上偏移了几十像素,多次点击还会累加,最终被导航条遮盖。

bug.png

最终发现是由于

    self.tableView.tableHeaderView = self.searchController.searchBar
    self.definesPresentationContext = true

这两行代码顺序写反了导致的。

这里解决了在模拟器里的问题。不过真机依然会有

最后发现 如果使用了IQ插件 并全局设置了
IQKeyboardManager.sharedManager().enable = true
之后 会导致真机会有这个问题。

下面贴下完整代码

var tableView: UITableView!

var searchController: UISearchController!

let cities = ["New York, NY", "Los Angeles, CA", "Chicago, IL", "Houston, TX",
                         "Philadelphia, PA", "Phoenix, AZ", "San Diego, CA", "San Antonio, TX",
                         "Dallas, TX", "Detroit, MI", "San Jose, CA", "Indianapolis, IN",
                         "Jacksonville, FL", "San Francisco, CA", "Columbus, OH", "Austin, TX",
                         "Memphis, TN", "Baltimore, MD", "Charlotte, ND", "Fort Worth, TX"]

var searchArr: [String] = [String](){
    didSet {
        // 重設 searchArr 後重整 tableView
        self.tableView.reloadData()
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    setupUI()
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.perform(#selector(AddressChoiceViewController.searchBarBecomeFirstResponder), with: nil, afterDelay: 0.01)
}
func setupUI() {
    view.backgroundColor = UIColor(Color_GlobalBackground)
    title = "当前城市-深圳"
    // 建立 UITableView
    self.tableView = UITableView(frame: CGRect(
        x: 0, y: 0,
        width: ScreenWidth,
        height: ScreenHeight),style: .plain)
    self.tableView.register(UITableViewCell.self,forCellReuseIdentifier: "Cell")
    self.tableView.delegate = self
    self.tableView.dataSource = self
    self.view.addSubview(self.tableView)
    
    
    // 建立 UISearchController 並設置搜尋控制器為 nil
    self.searchController = UISearchController(searchResultsController: nil)
    
    // 將更新搜尋結果的對象設為 self
    self.searchController.searchResultsUpdater = self
    
    // 搜尋時是否隱藏 NavigationBar
    // 這個範例沒有使用 NavigationBar 所以設置什麼沒有影響
    self.searchController.hidesNavigationBarDuringPresentation = true
    
    // 搜尋時是否使用燈箱效果 (會將畫面變暗以集中搜尋焦點)
    self.searchController.dimsBackgroundDuringPresentation = true
    
    // 搜尋框的樣式
    self.searchController.searchBar.searchBarStyle = .prominent
    
    // 設置搜尋框的尺寸為自適應
    // 因為會擺在 tableView 的 header
    // 所以尺寸會與 tableView 的 header 一樣
    
    self.searchController.searchBar.sizeToFit()
    
    // 將搜尋框擺在 tableView 的 header
    self.tableView.tableHeaderView = self.searchController.searchBar
    
    self.definesPresentationContext = true
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func backButtonAction() {
    navigationController?.pop(animated: true)
}
func searchBarBecomeFirstResponder(){
    self.searchController.searchBar.becomeFirstResponder()
}

Delegate 方法 自己实现下就行了。

相关文章

  • searchController 使用出现的懵逼bug

    今天在做搜索功能的时候,用到了searchController参考了http://blog.csdn.net/lo...

  • bug日记-Unexpected scopes found in

    镇楼 这么懵逼的bug,得压压惊 概述 有天你发现这个bug 无缘无故的就出现了,一脸懵逼,全部报错信息如下 原因...

  • searchcontroller使用

    #import"TableViewController.h" @interfaceTableViewControl...

  • 我就是我

    “懵逼树上懵逼果 懵逼树下懵逼的我 我望着懵逼树上的懵逼果 一个懵逼果砸到懵逼的我 我吃完懵逼果 一脸懵逼” 性情...

  • (⊙o⊙)哇

    懵逼果 懵比树上懵逼果,懵逼树下你和我,懵逼树下排排坐,一人一个懵逼果,懵逼还有你和我,原来懵逼不止我,懵逼树下懵...

  • 0208-"饿懵圈了"

    饿 饿,饿,饿 曲项用刀割, 拔毛烧开水, 点火盖上锅。 懵逼国有懵逼路,懵逼路旁懵逼树,懵逼树上懵逼果,懵逼树下...

  • 斗觅生日快乐啊啊啊啊啊啊

    嗯? 懵逼的我 懵逼的上了简书, 懵逼的看着更新 懵逼的翻到了媚音的文 懵逼的发现斗觅生日 懵逼的大吃一惊 懵逼的...

  • 凡人轶事37:懵逼树之歌

    懵逼树上懵逼果,懵逼树下你和我。 摘果砍树挖新坟,坟里睡着懵逼人。 今生睡在懵逼坟,来世还做懵逼人。 懵逼世道懵逼...

  • 使用ImageLoader 报错ImageLoader Imag

    在使用ImageLoader 加载图片的时候,偶尔出现错误如下: 突然遇到,一脸懵逼;这个错误,不同的手机,还不一...

  • ios系统 iphone5C 下app自带浏览器safari不支

    测试MM提过来的bug,改改改懵逼.............. 原本信誓旦旦的觉得肯定不是safari浏览器的问题...

网友评论

      本文标题:searchController 使用出现的懵逼bug

      本文链接:https://www.haomeiwen.com/subject/agiemttx.html