Swift 4 – iOS 11search栏范围将不会显示,因为它应该
我有一个应用程序,它可以很好地工作,在iOS 10上没有任何问题,但是在iOS 11和Xcode Beta 5中,我有这个奇怪的问题,search栏的范围看起来像是从底部切下的。 (Xcode beta和iOS 11的所有版本都是一样的)如果有人能把我放在正确的方向,我真的很高兴。 谢谢
上面你可以看到当我按下汉堡包菜单button时看到的东西。 这里没有问题..search栏也有一个范围栏,当点击时显示。我没有设置它以编程方式显示。但我想这是它的默认行为时,范围button标题设置。
问题:
当我点击search栏input信息,我看到上面的屏幕。 在iOS 10上,我没有任何问题。 但是现在,在iOS 11中,无论我做什么,我都无法使它像iOS 10一样工作。search范围栏显示为从底部切下。
这是如何显示在iOS 10中,我希望它在iOS 11中。
屏幕的故事板视图
我在这里粘贴相关的代码。
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]) } }
编辑:这是如果我添加约束search栏会发生什么情况。 首先一切都好。
然后,当你点击search栏时,会发生这种情况。search栏移出屏幕。 看箭头。
但是,如果closures滑动菜单,然后重新打开它
一切正常,直到你点击取消button。 之后,你必须再次做这一切,看看search栏的工作。
代码的约束
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 2017video: WWDC 2017
你应该这样做:
if #available(iOS 11.0, *) { navigationItem.searchController = searchController navigationItem.hidesSearchBarWhenScrolling = false } else { tableView.tableHeaderView = searchController.searchBar }
然后做相应的修改
我能够通过用自定义的searchBar
replacesearchController
来解决这个问题。
let searchBar = UISearchBar(frame:CGRect(x: 0, y: 0, width: 266, height: 100))
要小心,不要忘记使用
searchBar.sizeToFit
里面searchBarShouldBeginEditing
和searchBarShouldEndEditing
否则你可能会有奇怪的UI问题,特别是如果你使用
searchBar.showsScopeBar = true
所以,如果你有类似的问题,摆脱searchController
并实现一个searchBar
及其委托方法。 它不会给你相同的scopeBaranimation,但至less可以工作。 此方法的另一个缺点是,如果您有searchBar.isTranslucent = true
,则可能会在searchBar.isTranslucent = true
看到另一个scopeBar的重影。 所以确保
searchBar.isTranslucent = false
如果有人想出一个更好的方法来解决这个问题,我都耳…
示例代表设置
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { searchBar.showsScopeBar = true searchBar.sizeToFit() searchBar.setShowsCancelButton(true, animated: true) return true } func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool { searchBar.showsScopeBar = false searchBar.sizeToFit() searchBar.setShowsCancelButton(false, animated: true) return true }
这可能会解决你的问题:
func searchDisplayController(_ controller: UISearchController, willShowSearchResultsTableView tableView: UITableView) { tableView.backgroundColor = self.tableView.backgroundColor tableView.rowHeight = self.tableView.rowHeight tableView.tableFooterView = UIView() }