无法更改 UISearchBar 的高度
Can't Change Height of UISearchBar
我一直在搜索如何更改 UISearchBar 中文本字段的高度。这是我当前的代码,它不符合我的要求。我想知道我需要更改什么才能获得所需的结果。
搜索栏初始化
private var searchBar: UISearchBar = {
let search = UISearchBar()
search.translatesAutoresizingMaskIntoConstraints = false
search.searchBarStyle = .default
search.barTintColor = .none
search.placeholder = "Search project"
return search
}()
搜索栏约束(在 viewDidLoad()
中调用)
searchBar.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
searchBar.topAnchor.constraint(equalTo: view.topAnchor, constant: 30).isActive = true
searchBar.heightAnchor.constraint(equalToConstant: 42).isActive = true
searchBar.widthAnchor.constraint(equalToConstant: 265).isActive = true
搜索栏样式(在 viewDidLayoutSubviews()
中调用)
super.viewDidLayoutSubviews()
self.searchBar.layoutIfNeeded()
self.searchBar.layoutSubviews()
let searchTextField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField
var currentTextFieldBounds = searchTextField?.bounds
currentTextFieldBounds?.size.height = 130
searchTextField?.bounds = currentTextFieldBounds ?? CGRect.zero
searchBar.layoutIfNeeded()
searchBar.layoutSubviews()
我设法通过添加背景图像增加了 UISearchBar 的高度,调整了整个搜索栏框架的大小,然后设法通过更改字体大小调整了 textField 的大小。
首先更改搜索栏高度限制:
searchBar.heightAnchor.constraint(equalToConstant: 130).isActive = true
创建将以编程方式为搜索栏创建背景图像的函数:
func getImageWithCustomColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
然后更改 viewDidLayoutSubviews 的字体大小:
override func viewDidLayoutSubviews() {
self.searchBar.layoutIfNeeded()
self.searchBar.layoutSubviews()
self.searchBar.searchTextField.font = .systemFont(ofSize: 60.0)
//Your custom text size
searchBar.layoutIfNeeded()
searchBar.layoutSubviews()
}
最后,在 viewDidLoad 中创建一个与所需 textField 自定义大小(您的情况为 130)相同大小的图像,并调用该函数将所需颜色的背景图像放置在搜索栏上(本例中为清除背景):
let backgroundImage = getImageWithCustomColor(color: UIColor.clear, size: CGSize(width: 265, height: 130))
searchBar.setSearchFieldBackgroundImage(backgroundImage, for: .normal)
这是输出
希望对您有所帮助。
我一直在搜索如何更改 UISearchBar 中文本字段的高度。这是我当前的代码,它不符合我的要求。我想知道我需要更改什么才能获得所需的结果。
搜索栏初始化
private var searchBar: UISearchBar = {
let search = UISearchBar()
search.translatesAutoresizingMaskIntoConstraints = false
search.searchBarStyle = .default
search.barTintColor = .none
search.placeholder = "Search project"
return search
}()
搜索栏约束(在 viewDidLoad()
中调用)
searchBar.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
searchBar.topAnchor.constraint(equalTo: view.topAnchor, constant: 30).isActive = true
searchBar.heightAnchor.constraint(equalToConstant: 42).isActive = true
searchBar.widthAnchor.constraint(equalToConstant: 265).isActive = true
搜索栏样式(在 viewDidLayoutSubviews()
中调用)
super.viewDidLayoutSubviews()
self.searchBar.layoutIfNeeded()
self.searchBar.layoutSubviews()
let searchTextField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField
var currentTextFieldBounds = searchTextField?.bounds
currentTextFieldBounds?.size.height = 130
searchTextField?.bounds = currentTextFieldBounds ?? CGRect.zero
searchBar.layoutIfNeeded()
searchBar.layoutSubviews()
我设法通过添加背景图像增加了 UISearchBar 的高度,调整了整个搜索栏框架的大小,然后设法通过更改字体大小调整了 textField 的大小。
首先更改搜索栏高度限制:
searchBar.heightAnchor.constraint(equalToConstant: 130).isActive = true
创建将以编程方式为搜索栏创建背景图像的函数:
func getImageWithCustomColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
然后更改 viewDidLayoutSubviews 的字体大小:
override func viewDidLayoutSubviews() {
self.searchBar.layoutIfNeeded()
self.searchBar.layoutSubviews()
self.searchBar.searchTextField.font = .systemFont(ofSize: 60.0)
//Your custom text size
searchBar.layoutIfNeeded()
searchBar.layoutSubviews()
}
最后,在 viewDidLoad 中创建一个与所需 textField 自定义大小(您的情况为 130)相同大小的图像,并调用该函数将所需颜色的背景图像放置在搜索栏上(本例中为清除背景):
let backgroundImage = getImageWithCustomColor(color: UIColor.clear, size: CGSize(width: 265, height: 130))
searchBar.setSearchFieldBackgroundImage(backgroundImage, for: .normal)
这是输出
希望对您有所帮助。