UISearchBar最小风格具有透明背景

时间:2016-02-01 17:07:54

标签: uisearchbar transparent uisearchcontroller

我已将UISearchBar添加到UITableView的顶部。

        // init search bar
    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
        controller.searchBar.tintColor = Config.grayColor()
        controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal

        self.tableView.tableHeaderView = controller.searchBar

        // set content offset for table view in order
        // that the searchbar is hidden at the beginning
        self.tableView.contentOffset = CGPoint(x: 0, y: controller.searchBar.frame.size.height)

        return controller
    })()

这基本上看起来像预期的那样:

enter image description here

但是当我进入搜索文本区域并向下滚动表格视图时,它看起来很奇怪。搜索控制器的背景是透明的。

enter image description here

我已尝试设置barTintColorbackgroundColor,但它没有任何效果。

3 个答案:

答案 0 :(得分:2)

将searchBar的backgroundColor设置为白色

searchBar.backgroundColor = UIColor.white

答案 1 :(得分:0)

我测试了几乎所有东西。对我有用的唯一方法是:

searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.setBackgroundImage(UIImage(color: .white), for: UIBarPosition(rawValue: 0)!, barMetrics:.default)

这适用于“正常”和“呈现”形式。 UIImage(color: .white)只是一种创建1x1彩色图像的方法,用于填充背景。您可以找到并实施here

答案 2 :(得分:0)

您可以使用此代码。这对我有用:

UITextField.appearance(whenContainedInInstancesOf:  
      [UISearchBar.self]).backgroundColor = UIColor.RGB(red: 230, green: 230, blue: 230)

UISearchBar具有UITextField属性,您只需像上面那样更改颜色即可。