如何在swift中更改搜索栏的高度

时间:2018-03-02 07:12:49

标签: ios swift uitextfield uisearchbar

我一直在使用imageView上没有导航的搜索栏。 搜索栏高度是固定的,但我想更改搜索栏高度。

所以我试过

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

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

但它们不起作用。 我正在使用此代码

searchBar.isTranslucent = true
searchBar.searchBarStyle = .minimal    

所以喜欢这个

enter image description here

请帮我改变搜索栏文本字段的高度。

4 个答案:

答案 0 :(得分: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
    }
}

答案 1 :(得分:1)

尝试一下:

searchBar.frame.size.height = 44

答案 2 :(得分:0)

试试这个!

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

答案 3 :(得分:-1)

如果要与界面构建器一起使用:

class MediaSearchBar: UISearchBar {
    override func layoutSubviews() {

    }
}

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