Swift 4 - iOS 11 搜索栏范围不会像它应该的那样出现 [英] Swift 4 - iOS 11 Search bar scope won't appear as it should

查看:27
本文介绍了Swift 4 - iOS 11 搜索栏范围不会像它应该的那样出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 iOS 10 上运行良好且没有任何问题的应用程序,但是对于 iOS 11 和 Xcode Beta 5,我在搜索栏范围方面遇到了这个奇怪的问题,范围栏似乎是从底部切下来的.(所有版本的 Xcode beta 和 iOS 11 都是一样的)如果有人能让我朝着正确的方向发展,我真的很高兴.谢谢

I have an app which works nice and without any problems on iOS 10 but with iOS 11 and Xcode Beta 5, I have this strange problem with search bar scope where the scope bar seems like it was cut from the bottom. (It was the same with all versions of Xcode beta and iOS 11) I would really really be glad if someone can put me in the right direction. Thanks

在上面,您可以看到我按下汉堡菜单按钮时看到的内容.这里没问题..搜索栏还有一个范围栏,点击时会显示.我没有将它设置为以编程方式显示.但我想这是设置范围按钮标题时的默认行为.

Above you can see what I see when I press the hamburger menu button. No problem here.. searchbar also has a scopebar which shows up when clicked on.I did not set it to show up programatically.But I guess this is its default behaviour when scope button titles are set.

问题:

当我点击搜索栏输入信息时,我会看到上面的屏幕.在 iOS 10 上我没有问题.但是现在,在 iOS 11 中,无论我做什么,我都无法让它像在 iOS 10 上一样工作.搜索范围栏显示为从底部切下.

When I click on the searchbar to enter information I see the screen above. On iOS 10 I had no problems. But now, with iOS 11, whatever I do I just cannot make it work like it works on iOS 10. Search scope bar shows up like it was cut from the bottom.

这就是它在 iOS 10 中的显示方式,我希望它在 iOS 11 中出现.

This is how it shows up in iOS 10 and I want it to be in iOS 11.

屏幕的故事板视图

我在这里粘贴相关代码.

I am pasting the relevant code here.

    class SlideMenuViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchControllerDelegate,UIGestureRecognizerDelegate{

     let searchController = UISearchController(searchResultsController: nil)

    @IBOutlet var sview: UIView!

    @IBOutlet var tableView: UITableView!



override func viewWillAppear(_ animated: Bool) {
//some non relevant code before this

 if sview.subviews .contains(searchController.searchBar) {
            print("Already contains")


        } else {

            sview.addSubview(searchController.searchBar)
            print(searchController.searchBar.showsScopeBar)

            searchController.searchBar.sizeToFit()
            searchController.searchBar.frame.size.width = view.frame.size.width



          searchController.searchBar.barTintColor = searchBarTintColor

            searchController.searchBar.tintColor = searchTintColor
        }



    }


override func viewDidLoad() {

//some other code

searchController.searchBar.delegate = self

searchController.searchBar.scopeButtonTitles = [NSLocalizedString("İsimlerde", comment: ""), NSLocalizedString("Açıklamalarda", comment: "")]

        searchController.searchBar.returnKeyType = UIReturnKeyType.done

searchController.searchResultsUpdater = self

        searchController.dimsBackgroundDuringPresentation = false
        definesPresentationContext = true

//some other code

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

     if searchController.isActive && searchController.searchBar.text != "" {

        switch scpglobal {
            case NSLocalizedString("İsimlerde", comment: ""):
                return filteredIsimler.count

            case NSLocalizedString("Açıklamalarda", comment: ""):
                return filteredAciklamalar.count

            default:
                print("Tableview default")
        }

        }



        return isimlerArray.count
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

//some code about search here but nothing that will result in that behaviour

}

 func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        print("cancel")
        searchController.searchBar.text = ""

    }


func updateSearchResults(for searchController: UISearchController) {


        let searchBar = searchController.searchBar



        let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
        filterContentForSearchText(searchText: searchController.searchBar.text!, scope: scope)
        tableView.reloadData()
    }

}


extension SlideMenuViewController:UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
        filterContentForSearchText(searchText: searchController.searchBar.text!, scope: searchController.searchBar.scopeButtonTitles![selectedScope])
    }
}

如果我向搜索栏添加约束,就会发生这种情况.首先一切似乎都很好.

This is what happens if I add constraints to search bar. First everything seems good.

然后当您单击搜索栏时,会发生这种情况..搜索栏移出屏幕.见箭头.

Then when you click on search bar this happens..Search bar moves out of the screen. See the arrow.

但是如果您关闭滑动菜单并重新打开它

But if you close the sliding menu and re-open it then

一切正常,直到您单击取消按钮.之后,您必须再次执行所有这些操作才能看到搜索栏正常工作.

Everything works OK until you click the cancel button. After that you have to do all this again to see the search bar working.

约束代码

 searchController.searchBar.translatesAutoresizingMaskIntoConstraints = false
 sview.addConstraint(NSLayoutConstraint(item: searchController.searchBar, attribute: .top, relatedBy: .equal, toItem: sview, attribute: .top, multiplier: 1, constant: 0))
 sview.addConstraint(NSLayoutConstraint(item: searchController.searchBar, attribute: .bottom, relatedBy: .equal, toItem: sview, attribute:.bottom, multiplier: 1, constant: 0))

 sview.addConstraint(NSLayoutConstraint(item: searchController.searchBar, attribute: .leading, relatedBy: .equal, toItem: sview, attribute: .leading,multiplier: 1, constant: 0))
 sview.addConstraint(NSLayoutConstraint(item: searchController.searchBar, attribute: .trailing, relatedBy: .equal, toItem: sview, attribute: .trailing, multiplier: 1, constant: 0))

推荐答案

查看 这个 WWDC 2017 视频.

你应该这样做:

if #available(iOS 11.0, *) {
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false
} else {
    tableView.tableHeaderView = searchController.searchBar
}

然后进行相应的更改.

这篇关于Swift 4 - iOS 11 搜索栏范围不会像它应该的那样出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆