1

I have been making search bars without navigation on the imageView. The search bar height is fixed but i want to change the search bar height.

so i tried

let frame = CGRect(x: 0, y: 0, width: 100, height: 44)
searchbar.frame = frame 

and

searchbar.heightAnchor.constraint(equalToConstant: 200).isActive = true

but they don't work. I'm using this code

searchBar.isTranslucent = true
searchBar.searchBarStyle = .minimal    

so like this

enter image description here

please help me change the search bar textfield height.

thlpswm
  • 45
  • 3
chen
  • 39
  • 1
  • 2
  • 6
  • Possible duplicate of [Can the height of the UISearchbar TextField be modified?](https://stackoverflow.com/questions/30858969/can-the-height-of-the-uisearchbar-textfield-be-modified) – cb89 Mar 02 '18 at 07:53
  • Using `searchbar.heightAnchor.constraint(equalToConstant: 200).isActive = true` worked for me. How did you create the search bar? I added it to my storyboard with the top, leading and trailing anchors set to the superview edges. That's it, no other fancy magic. – Guy Kogus Mar 02 '18 at 09:44
  • you should customize its appearance by embedding it within a container view or modifying its superview's layout – Alirza Eram May 17 '23 at 21:46

5 Answers5

2
fileprivate func customizeSearchField(){
    UISearchBar.appearance().setSearchFieldBackgroundImage(UIImage(), for: .normal)
    searchField.backgroundColor = .white
    if let searchTextField = searchField.value(forKey: "searchField") as? UITextField {
        searchTextField.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            searchTextField.heightAnchor.constraint(equalToConstant: 50),
            searchTextField.leadingAnchor.constraint(equalTo: searchField.leadingAnchor, constant: 10),
            searchTextField.trailingAnchor.constraint(equalTo: searchField.trailingAnchor, constant: -10),
            searchTextField.centerYAnchor.constraint(equalTo: searchField.centerYAnchor, constant: 0)
        ])
        searchTextField.clipsToBounds = true
        searchTextField.font = GenericUtility.getOpenSansRegularFontSize(14)
        searchTextField.layer.cornerRadius = 4.0
        searchTextField.layer.borderWidth = 1.0
        searchTextField.layer.borderColor = AppColor.primaryLightGray.cgColor
    }
}
eildiz
  • 487
  • 1
  • 7
  • 14
1

try this!

    for myView in searchBars.subviews  {
        for mySubView in myView.subviews  {
            if let textField = mySubView as? UITextField {
                 var bounds: CGRect
            bounds = textField.frame
            bounds.size.height = 40 //(set your height)
                textField.bounds = bounds
                textField.borderStyle = UITextBorderStyle.RoundedRect
            }
        }
    }
Shital Sharma
  • 17
  • 1
  • 5
remyr3my
  • 768
  • 1
  • 6
  • 19
  • thanks for your help. but.. it doesn't work. i modify your subsubview -> mySubView. Actually, i don't understand subsubview . – chen Mar 02 '18 at 08:33
  • did not work for me, too& It goes into TextField updates the frame but ob screen i get the same searchbar as before – Gulz Apr 10 '18 at 05:23
0

I found better way without hijacking objc api.

  1. Make square image with desired height. In my case I've made pink square 40x40. Also I've made it with corner radius, so there is no need to specify corner radius for UITextField
  2. Add image to assets
  3. Specify slicing mode for you image (in my case I've also added 12pt for left and right because of corner radius. You can set left and right 1 and if you don't have corner radius) Slicing mode in asset folder
  4. In code use searchBar.setSearchFieldBackgroundImage(UIImage(named: "pinkbg"), for: .normal)
Alexey Lysenko
  • 1,890
  • 2
  • 12
  • 28
-1

Try this:

searchBar.frame.size.height = 44
Shubham Mishra
  • 1,303
  • 13
  • 24
-1

if you want to use with interface builder:

class MediaSearchBar: UISearchBar {
    override func layoutSubviews() {

    }
}

and setup it in viewDidLoad:

func setupSearchBar() {
    searchBar.delegate = self
    for myView in searchBar.subviews {
        for mySubView in myView.subviews {
            if let textField = mySubView as? UITextField {
                textField.translatesAutoresizingMaskIntoConstraints = false
                NSLayoutConstraint.activate([
                    textField.heightAnchor.constraint(equalTo: myView.heightAnchor,
                                                      multiplier: 1.0, constant: -20.0),
                    textField.leadingAnchor.constraint(equalTo: myView.leadingAnchor, constant: 10.0),
                    textField.trailingAnchor.constraint(equalTo: myView.trailingAnchor, constant: -10.0),
                    textField.centerYAnchor.constraint(equalTo: myView.centerYAnchor, constant: 0.0)
                ])
                textField.clipsToBounds = true
            }
        }
    }
}
mehdi
  • 2,703
  • 1
  • 13
  • 15