gpt4 book ai didi

ios - 为 UISearchBar textField 添加底部边框

转载 作者:搜寻专家 更新时间:2023-11-01 06:27:35 24 4
gpt4 key购买 nike

我有一个 UISearchBar 添加到 navigationItem 的 titleView。我想向 UISearchBar 的文本字段添加底部边框。

当尝试添加图层时,没有任何显示。

extension UITextField {

func underlined() {

let border = CALayer()
let width = CGFloat(3.0)
border.borderColor = UIColor.red.cgColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: width)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true

}

我试过这段代码来获取 textField 的内容,但没有成功。

let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as? UITextField

textFieldInsideSearchBar?.borderStyle = .none
textFieldInsideSearchBar?.textColor = UIColor.white
textFieldInsideSearchBar?.underlined()

非常感谢您的帮助!

最佳答案

那是因为在 viewDidLoad 方法中,框架还没有正确配置,试试这个:

 override func viewWillAppear(_ animated: Bool) {
let textFieldInsideSearchBar = searchBar.value(forKey: "_searchField") as? UITextField
textFieldInsideSearchBar?.borderStyle = .none
textFieldInsideSearchBar?.textColor = UIColor.white
textFieldInsideSearchBar?.underlined()
}

关于ios - 为 UISearchBar textField 添加底部边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51916521/

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