【问题标题】:How to fix UISearchController search bar resizing when focused?聚焦时如何修复 UISearchController 搜索栏调整大小?
【发布时间】:2020-05-26 22:14:25
【问题描述】:

我正在尝试在一个像这样的小弹出屏幕内实现一个带有 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.
}

【问题讨论】:

  • 你试过self.definesPresentationContext = true吗?
  • 是的,它没有解决问题。
  • 从iOS 11开始,你应该使用self.navigationItem.searchController = searchController而不是tableView.tableHeaderView = searchController.searchBarsee this answer了解详情。

标签: ios swift xcode uisearchcontroller visual-glitch


【解决方案1】:

使用UISearchBar 而不是UISearchController

类似:

let searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 44))

希望这对某人有所帮助!

【讨论】:

    猜你喜欢
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多