gpt4 book ai didi

ios - 搜索栏文本字段颜色不会改变

转载 作者:行者123 更新时间:2023-11-28 15:15:09 26 4
gpt4 key购买 nike

当我尝试更改搜索栏的 textfield 的文本颜色或占位符文本颜色时,没有任何反应。

文本字段嵌入在搜索 Controller 的搜索栏中。

这是我正在使用的代码:

let textField = self.searchController.searchBar.value(forKey: "searchField") as! UITextField

textField.textColor = .white

我觉得有趣的是,当我尝试更改色调时,它起作用了:

textField.tintColor = .white

有谁知道为什么会发生这种情况?

我正在尝试将文本和占位符文本颜色设为白色。

谢谢!

最佳答案

这是你如何做的:

let textField = searchBar.value(forKey: "searchField") as? UITextField
textField?.textColor = .white
textField?.attributedPlaceholder = NSAttributedString(string: "Placeholder", attributes: [NSForegroundColorAttributeName: UIColor.white])

因此,您首先获取 textField 并设置 textColor,然后为您的 textField 设置 attributedPlaceholder .

更新:
对于您的 searchController,只需执行以下操作:

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

let textField = self.searchController.searchBar.value(forKey: "searchField") as? UITextField
textField?.textColor = .white
textField?.attributedPlaceholder = NSAttributedString(string: "1334", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
}

这会给你: enter image description here enter image description here

关于ios - 搜索栏文本字段颜色不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46991511/

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