I\'m testing out my UI and I find the search bar a little bit too narrow for my liking. I also want to make sure people with poorer vision or poorer manual dexterity have n
This is the correct Swift 4.0 answer on how to change the height of a UISearchBar:
let image = getImageWithColor(color: UIColor.clear, size: CGSize(width: 1, height: 40))
searchBar.setSearchFieldBackgroundImage(image, for: .normal)
Using following method:
func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
Tested on 4th. June. 2018
Update for Swift 4.2/iOS 12.1:
let newHeight : CGFloat = 45
for subview in searchBar.subviews {
for subsubview in subview.subviews {
if let textField = subsubview as? UITextField {
var currentTextfieldBounds = textField.bounds
currentTextfieldBounds.size.height = newHeight
textField.bounds = currentTextfieldBounds
textField.borderStyle = .roundedRect
textField.contentVerticalAlignment = .center
}
}
}