在UISearchController iOS 11上使用背景图像

时间:2018-01-22 08:53:25

标签: swift uinavigationcontroller uisearchcontroller

我正在为UISearchController实施UITableView,但我正在努力解决iOS 11的自定义问题。我的导航栏使用的是我希望搜索控制器匹配的渐变图像背景,但我还没有找到一种方法来设置UISearchController的背景图像。它在UISearchController上作为TableHeaderView完美地运行,但在iOS 11中几乎没有传递任何自定义。

当前结果:

Current outcome

期望的结果:

Desired outcome

这是我正在使用的代码: (在viewDidLoad上调用)

private func setupSearchController() {

    let searchController = UISearchController(searchResultsController: nil)

    // Convert CAGradientLayer to UIImage
    let gradient = Color.blue.gradient
    gradient.frame = searchController.searchBar.bounds
    UIGraphicsBeginImageContext(gradient.bounds.size)
    gradient.render(in: UIGraphicsGetCurrentContext()!)
    let gradientImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    // Setup the Search Controller
    searchController.searchBar.backgroundImage = gradientImage
    searchController.searchBar.isTranslucent = false
    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = false
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search by transaction name"
    searchController.searchBar.tintColor = Color.custom(hexString: "FAFAFA", alpha: 1).value
    definesPresentationContext = true
    searchController.searchBar.delegate = self

    // Implement Search Controller
    if #available(iOS 11.0, *) {
        navigationItem.searchController = searchController
        navigationItem.hidesSearchBarWhenScrolling = false
        navigationController?.navigationBar.setBackgroundImage(gradientImage, for: .default)
    } else {
        tableView.tableHeaderView = searchController.searchBar
    }

}

2 个答案:

答案 0 :(得分:4)

感谢Krunal's answer我在搜索了很长时间后找到了一个不错的解决方案。

这就是我实现iOS 11背景图像的方法:

    // Implement Search Controller
    if #available(iOS 11.0, *) {
        if let navigationBar = self.navigationController?.navigationBar {
            navigationBar.barTintColor = UIColor(patternImage: gradientImage!)
        }
        if let textField = searchController.searchBar.value(forKey: "searchField") as? UITextField {
            if let backgroundview = textField.subviews.first {
                backgroundview.backgroundColor = UIColor.white
                backgroundview.layer.cornerRadius = 10;
                backgroundview.clipsToBounds = true;

            }
        }
        navigationItem.searchController = searchController
        navigationItem.hidesSearchBarWhenScrolling = false
    } else {
        tableView.tableHeaderView = searchController.searchBar
    }

答案 1 :(得分:0)

您可以为此添加CustomView。 使用CustomView将searchBar添加到它,然后将此customView添加到ViewController。 现在,您可以轻松地将此customView的背景设置为:

//if customView is named as customView

    let backgroundImage = UIImageView(frame: UIScreen.mainScreen().bounds)
    backgroundImage.image = UIImage(named: "background.png")
    self.customView.insertSubview(backgroundImage, atIndex: 0)