如何在 swift 中更改搜索栏的高度
how to change height of search bar in swift
我一直在 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
这么喜欢
请帮我更改搜索栏文本字段的高度。
试试这个!
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
}
}
}
试试这个:
searchBar.frame.size.height = 44
如果您想与界面生成器一起使用:
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
}
}
}
}
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
}
}
我一直在 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
这么喜欢
请帮我更改搜索栏文本字段的高度。
试试这个!
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
}
}
}
试试这个:
searchBar.frame.size.height = 44
如果您想与界面生成器一起使用:
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
}
}
}
}
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
}
}