聚焦时如何修复 UISearchController 搜索栏大小调整?
How to fix UISearchController search bar resizing when focused?
我正在尝试在一个小弹出屏幕内实现一个带有 UITableView 的 UISearchController,如下所示:
Image displaying a popup with a UITableView and UISearchController.
但是,当搜索栏获得焦点时,它会向上移动并变宽,如下所示:
Image displaying a glitched search bar.
我已经找到另一个 post 关于这个问题 ,但公认的解决方案似乎没有解决问题。
作为参考,我正在使用全屏显示的模态转场。有一个内容视图,其中 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))
希望这对某人有所帮助!
我正在尝试在一个小弹出屏幕内实现一个带有 UITableView 的 UISearchController,如下所示:
Image displaying a popup with a UITableView and UISearchController.
但是,当搜索栏获得焦点时,它会向上移动并变宽,如下所示:
Image displaying a glitched search bar.
我已经找到另一个 post 关于这个问题
作为参考,我正在使用全屏显示的模态转场。有一个内容视图,其中 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))
希望这对某人有所帮助!