0

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
  • 29
  • 1
  • 2
  • 5
  • 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

4 Answers4

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
  • 459
  • 1
  • 6
  • 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
  • 707
  • 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
-1

Try this:

searchBar.frame.size.height = 44
Shubham Mishra
  • 1,195
  • 12
  • 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,335
  • 1
  • 12
  • 12