chen
chen

Reputation: 39

how to change height of search bar in swift

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.

Upvotes: 1

Views: 14940

Answers (5)

Alexey Lysenko
Alexey Lysenko

Reputation: 1976

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)

Upvotes: 0

eildiz
eildiz

Reputation: 537

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
    }
}

Upvotes: 5

mehdi
mehdi

Reputation: 2953

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
            }
        }
    }
}

Upvotes: -1

remyr3my
remyr3my

Reputation: 778

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
            }
        }
    }

Upvotes: 1

Shubham Mishra
Shubham Mishra

Reputation: 1331

Try this:

searchBar.frame.size.height = 44

Upvotes: -1

Related Questions