UISearchBar minimal style has transparent background
Asked Answered
A

4

11

I've a UISearchBaradded to the top of an 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
    })()

This basically looks like expected:

enter image description here

But when I enter the search textfield and scroll down the table view it looks weird. The background of the search controller is transparent.

enter image description here

I've tried to set barTintColor and backgroundColor but it doesn't have any effect.

Astaire answered 1/2, 2016 at 17:7 Comment(2)
Any updates on this?Cole
#57746792Vivianviviana
B
2

set the searchBar's backgroundColor to white

searchBar.backgroundColor = UIColor.white

Bonni answered 22/9, 2017 at 15:53 Comment(1)
Welcome to StackOverflow and thanks for your help. You might want to make your answer even better by adding some explanation.Stateless
C
2

I tested almost everything. The only way that worked for me is:

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

This works in both "normal" and "presented" forms. UIImage(color: .white) is just a way to create a 1x1 colored image that will be used to fill the background. You can find and implementation here

Compote answered 13/3, 2018 at 13:51 Comment(0)
P
0

You can use this code. This works for me:

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

UISearchBar has a UITextField property and you just change that color like above.

Pandect answered 14/4, 2019 at 14:23 Comment(0)
N
0

the only solution that worked for me in Swift 5:

func viewDidLoad() {
  // make content disappear when it touches searchBar
  edgesForExtendedLayout = []

  // make searchBar color non-transparent
  searchBarController.searchBar.backgroundColor = .red
}

Nataline answered 18/8, 2023 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.