gpt4 book ai didi

swift - 如何自定义UISearchController?

转载 作者:行者123 更新时间:2023-11-30 11:04:21 27 4
gpt4 key购买 nike

我在我的应用程序中使用UISearchController,但我无法找到自定义它的方法。我在 Stackoverflow 上查看过,但没有一个已确认的答案对我有用。我尝试寻找更多地方,但没有任何效果。这是我的代码:

    import UIKit

class MainVC: UIViewController {


lazy var mSearchBarController: UISearchController = {
let mSearchBar = UISearchController(searchResultsController: nil)
mSearchBar.searchBar.barStyle = .default
mSearchBar.searchBar.placeholder = "enter city here"
mSearchBar.searchBar.tintColor = UIColor.white

return mSearchBar
}()

override func viewDidLayoutSubviews() {
setupSearchBar()
}
override func viewDidLoad() {

super.viewDidLoad()
view.backgroundColor = UIColor(red: 80/255, green: 135/255, blue: 179/255, alpha: 1.0)
setupNavBar()
self.navigationItem.searchController = mSearchBarController

}

private func setupNavBar(){
navigationItem.title = "Home"
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.hidesSearchBarWhenScrolling = true
}

private func setupSearchBar(){
let mSearchTextField = mSearchBarController.searchBar.value(forKey: "searchField") as? UITextField
mSearchTextField?.textColor = UIColor(red: 255/255, green: 245/255, blue: 139/255, alpha: 1.0)

let mAttributedPlaceholder = NSMutableAttributedString(string: "enter city here", attributes: [NSAttributedString.Key.foregroundColor : UIColor(red: 255/255, green: 245/255, blue: 139/255, alpha: 1.0)])
mSearchTextField?.attributedPlaceholder = mAttributedPlaceholder

for view in mSearchBarController.searchBar.subviews {
for subview in view.subviews {
if let textField = subview as? UITextField {
textField.backgroundColor = UIColor.red
textField.textColor = UIColor.white
}
}
}
}
}

我无法找到更改 searchBartextColor 及其 backgroundColor 的方法。这是我得到的:

enter image description here

最佳答案

看到我使用这个简单的代码来更改seacrhBar的textColorbackgroundColor:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var searchBar: UISearchBar!


override func viewDidLoad() {
super.viewDidLoad()


for view in searchBar.subviews {
for subview in view.subviews {
if let textField = subview as? UITextField {
textField.backgroundColor = UIColor.red
textField.textColor = UIColor.white
}
}
}
}

作为引用,您可以检查:-

How can I change the UISearchBar search text color? (对于文本颜色)

http://jitu1990.blogspot.com/2017/06/textcolor-and-backgroundcolor.html (背景颜色)

关于swift - 如何自定义UISearchController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52905371/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com