我正在尝试在一个小的弹出屏幕中使用UITableView实现UISearchController,如下所示:Image displaying a popup with a UITableView and UISearchController。但是,将搜索栏聚焦后,它会向上移动并变宽,如下所示:Image displaying a glitched search bar。我发现了有关此问题的另一篇文章here,但是公认的解决方案似乎无法解决该问题。作为参考,我正在使用全屏显示的模式搜索。有一个内容视图,其中对tableView和titleLabel进行了子视图。这是弹出视图控制器的相关代码。
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var titleLabel: UILabel!
var items = ["Test", "oewrghp", "wopqet", "vbsjadsf", "rweoghp", "bmwehth", "pqeojnh"]
var filteredItems = [String]()
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissTapGesture(gesture:)))
view.addGestureRecognizer(tapGesture)
view.backgroundColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 0.3)
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.barTintColor = .white
searchController.searchBar.backgroundColor = .clear
searchController.searchBar.delegate = self
searchController.obscuresBackgroundDuringPresentation = false
titleLabel.text = "Search"
tableView.tableHeaderView = searchController.searchBar
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()
tableView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:))))
// Do any additional setup after loading the view.
}
使用UISearchBar
代替UISearchController
类似:
let searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 44))