UISearchBar最小风格具有透明背景

问题描述 投票:8回答:3

我有一个UISearchBaradded到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,但它没有任何影响。

uisearchbar transparent uisearchcontroller
3个回答
2
投票

将搜索栏背景颜色设置为白色

searchBar.backgroundColor = UIColor.white


0
投票

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

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

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


0
投票

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

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

UISearchBar有一个UITextField属性,你只需改变上面那种颜色。

© www.soinside.com 2019 - 2024. All rights reserved.