我在UISearchBar中添加UINavigationController作为subView,但是subView的行为异常。它隐藏UITableView的节标题,当我单击它时它会垂直跳到中心。
swift - 为什么UISearchBar跳下来并在Swift中第一行覆盖/重叠?-LMLPHP
swift - 为什么UISearchBar跳下来并在Swift中第一行覆盖/重叠?-LMLPHP
如何将UISearchBar的背景色更改为橙色?我试过调色和背景色,但没用。

var searchController: UISearchController!
var searchResultController: UITableViewController!

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.isTranslucent = false

    navigationController?.navigationBar.prefersLargeTitles = true;
    navigationController?.navigationBar.largeTitleTextAttributes = [
        NSAttributedStringKey.foregroundColor : UIColor.white
    ]
    navigationController?.navigationBar.barTintColor = UIColor.orange

    searchController = UISearchController(searchResultsController: searchResultController)

    self.searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = false
    self.definesPresentationContext = true

    let searchBar = searchController.searchBar
    searchBar.searchBarStyle = .default
    searchBar.tintColor = UIColor.orange
    searchBar.backgroundColor = UIColor.orange

    view.addSubview(searchBar)
}

最佳答案

我在detail viewcontroller上添加了下面的一行以隐藏搜索栏,但这会导致搜索栏向下跳转。

self.definesPresentationContext = true

我删除了这一行,但是我在detail viewcontroller上有一个搜索栏,这就是我修复它的方法。
我将搜索栏变量传递给detail view控制器,并将其属性设置为hidden。我还添加了以下代码行,以便在用户返回到原始视图时将搜索栏带回来。
override func viewWillDisappear(_ animated: Bool) {
    searchBar.isHidden = false
}

下面的代码行修复了隐藏表视图的第一行或节标题的第二个问题。
self.tableView.contentInset = UIEdgeInsetsMake(50,0,0,0);

关于swift - 为什么UISearchBar跳下来并在Swift中第一行覆盖/重叠?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47169206/

10-14 20:11
查看更多