我使用UISearchController,我的项目就像这里,https://www.youtube.com/watch?v=h7caxpp3Xps和我试着在答案中设置textField背景:iOS 11 customise search bar in navigation bar如果我使用蓝,绿,白,黑这样的颜色都可以,但当我尝试设置另一种颜色时,我得到的结果是

另外,我在UISearchBarTextField中有两个_UISearchBarSearchFieldBackgroundView,其中一个用另一种颜色填充

这是苹果开发者的bug吗?
viewController - enter link description here的代码
发布于 2018-09-17 16:25:24
在UIColor扩展中创建类属性
extension UIColor
{
class var themeColor:UIColor {
return UIColor(red: 210.0/255.0, green: 105.0/255.0, blue: 130.0/255.0, alpha: 1.0)
}
}
OR
extension UIColor {
static let themeColor = UIColor(red: 210.0/255.0, green: 105.0/255.0, blue: 130.0/255.0, alpha: 1.0)
}
Usage
self.view.backgroundColor = UIColor.themeColor原始答案here
发布于 2018-09-17 19:30:20
您可以设置searchBar的色调和背景颜色。
controller.searchBar.barTintColor = UIColor.appThemeColor() controller.searchBar.backgroundColor = UIColor.white
controller.searchBar.tintColor = Color.theme.value发布于 2018-09-17 20:50:45
如果您想要,也可以尝试使用此代码来自定义其他内容
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
self.searchBar.customize()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension UISearchBar {
func customize(){
self.backgroundColor = UIColor.clear
self.backgroundImage = UIImage()
for subView in self.subviews {
for subsubView in subView.subviews {
if let textField = subsubView as? UITextField {
textField.clearButtonMode = .whileEditing
textField.backgroundColor = UIColor.red
textField.layer.cornerRadius = textField.bounds.size.height / 2
textField.layer.masksToBounds = true
textField.layer.borderColor = UIColor.black.cgColor
textField.layer.borderWidth = 1.0
}
}
}
self.setImage(UIImage(named: "searchfield")?.withRenderingMode(.alwaysTemplate), for: UISearchBarIcon.search, state: .normal)
self.setPositionAdjustment(UIOffset(horizontal: 10, vertical: 0), for: UISearchBarIcon.search)
self.searchTextPositionAdjustment = UIOffset(horizontal: 8, vertical: 0)
}
}https://stackoverflow.com/questions/52363502
复制相似问题