代码之家  ›  专栏  ›  技术社区  ›  nomadoda

在UISearchController iOS 11上使用背景图像

  •  4
  • nomadoda  · 技术社区  · 7 年前

    我正在实施 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 回复  |  直到 7 年前
        1
  •  6
  •   nomadoda    7 年前

    幸亏 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
        }
    
        2
  •  0
  •   Madhur    7 年前

    您可以为此添加自定义视图。 使用CustomView将搜索栏添加到其中,然后将此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)