Search code examples
iosswiftcolorsuisearchbar

How to set border bottom on UISearchBar in Swift3


I'd like to specify the bottom border color of a UISearchBar. I've seen a lot of answers that will help get rid of the border, or set the border color as a whole, but I just want to set the border color for the bottom border. Here is my code:

searchController.searchBar.layer.borderWidth = 1 searchController.searchBar.layer.borderColor = UIColor.white.cgColor

Is there a way to set borderColor for just the bottom border?


Solution

  • Just add a subview to your textField like this

    let frame = CGRectMake(0, textField.frame.size.height - 1, textField.frame.size.width, 1)
    let border = UIView(frame: frame)
    border.backgroundColor = UIColor.whiteColor()
    textField.addSubview(border)