我想更改UISearchbar的cornerRadius属性,但它不起作用。我尝试过:
if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = Color.text.primary // this is working
if let backgroundView = textfield.subviews.first {
backgroundView.layer.cornerRadius = 2 // not working
backgroundView.clipsToBounds = true
}
}有什么想法吗?如果我将backgroundView设置为另一种颜色,它将按预期工作(因此BackgroundView在此处)
发布于 2018-01-24 07:07:55
它看起来像是背景视图有自定义的渲染代码来强制圆角。您可以尝试的是隐藏该视图,并手动配置文本字段以使其外观相同。
if let textField = self.searchBar.subviews.first?.subviews.flatMap({ $0 as? UITextField }).first {
textField.subviews.first?.isHidden = true
textField.textColor = Color.text.primary
textField.layer.backgroundColor = UIColor.white.cgColor
textField.layer.cornerRadius = 2
textField.layer.masksToBounds = true
}请注意,如果苹果公司决定改变UISearchBar文本字段的结构方式,这可能会在未来的iOS版本中被打破。
编辑为不使用value(forKey:)。
https://stackoverflow.com/questions/48411613
复制相似问题