UISearchController Swift 4:间距问题

时间:2018-06-28 23:33:13

标签: ios uitableview swift4 uisearchbar uisearchcontroller

我正在添加UISearchController,但是我一直遇到间距问题。特别是,当我在搜索栏中进行搜索时,它会上移并在表格视图和搜索栏之间留出黑色空间。

Initial spacing issues

然后,当我键入内容时,黑色空间消失了,但是搜索栏和表格之间有很大的白色空间。

Search issues

我尝试添加以下行,但间距问题变得更糟:

searchController.hidesNavigationBarDuringPresentation = false

任何有用的提示将不胜感激。

这是我当前的代码:

class SearchViewController: UITableViewController {
var searchController: UISearchController!

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = UIColor.clear
view.isOpaque = false
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)

//Set up Table View
self.navigationController?.navigationBar.shadowImage = UIImage()
let searchResultsController = UITableViewController(style: .plain)
searchResultsController.tableView.delegate = self
searchResultsController.tableView.dataSource = self
searchResultsController.tableView.rowHeight = 65
searchResultsController.tableView.register(SearchCell.self, forCellReuseIdentifier: "SearchCell")

// Setup  Search Controller
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchBar.tintColor = UIColor.white
searchController.searchBar.barTintColor = UIColor(red: 34/255, green: 167/255, blue: 240/255, alpha: 1.0)
searchController.searchBar.layer.borderColor = UIColor(red: 34/255, green: 167/255, blue: 240/255, alpha: 1.0).cgColor
searchController.searchBar.layer.borderWidth = 1.00

tableView.tableHeaderView?.addSubview(searchController.searchBar)
let searchBar = searchController.searchBar
searchBar.sizeToFit()

searchBar.placeholder = "Search"
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
definesPresentationContext = true
searchController.dimsBackgroundDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
}

1 个答案:

答案 0 :(得分:0)

尝试添加

self.automaticallyAdjustsScrollViewInsets = false

self.extendedLayoutIncludesOpaqueBars = true

在viewDidload中。

或取消选中“调整滚动视图插图”