0

我正在向 tableView.tableHeaderView 添加一个 imageView 和 UISearchController.searchBar。但没有找到正确的方法在图像之后显示 searchBar。

    let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 333))
    let image = UIImageView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 269))
    image.image = #imageLiteral(resourceName: "map")
    view.addSubview(image)
    view.insertSubview(searchController.searchBar, at: 1)//insertSubview(searchController.searchBar, belowSubview: image)
    self.tableView.tableHeaderView = view

但结果是这样的。在此处输入图像描述 期望的结果就是这样。 在此处输入图像描述

4

2 回答 2

0

如果您想在视图底部添加搜索栏 您可以以编程方式添加搜索栏并将其固定到视图底部

view.addSubview(searchBar)

searchBar.bottomAnchor.constraint(equalTo: view.trailingAnchor).isActive =true
searchBar.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
searchBar.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
searchBar.heightAnchor.constraint(equalToConstant: 50).isActive = true

最简单的方法

于 2019-11-22T16:40:57.013 回答
0

如果您不想使用自动布局,请尝试提供搜索栏框架

 searchBar?.frame = CGRect(x: 0, y: view.frame.size.height-searchBar!.frame.size.height  , width: UIScreen.main.bounds.width, height: searchBar?.frame.size.height ?? 44)


If you are using auto-layout add bottom constraint to searchbar.
    view.addSubview(searchBar!)
searchBar!.translatesAutoresizingMaskIntoConstraints = false
searchBar!.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
searchBar!.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
searchBar!.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
searchBar!.heightAnchor.constraint(equalToConstant: 44).isActive = true
于 2019-11-22T17:09:25.540 回答