【问题标题】:Can't set correct background color for UITextField in UISearchController无法在 UISearchController 中为 UITextField 设置正确的背景颜色
【发布时间】:2018-09-17 08:20:06
【问题描述】:

我在我的项目中使用 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

【问题讨论】:

标签: ios swift xcode


【解决方案1】:

在 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

【讨论】:

  • 不,同样的结果
  • searchController.searchBar.isTranslucent = false,但相同 =(
  • 确保你使用正确的颜色表示,如果我知道检查听起来很愚蠢,但仔细检查一下,我过去有类似的问题,这几乎解决了我的想法已经用完了跨度>
【解决方案2】:

您可以设置searchBar的色调颜色和背景颜色。

controller.searchBar.barTintColor = UIColor.appThemeColor()controller.searchBar.backgroundColor = UIColor.white

controller.searchBar.tintColor = Color.theme.value

【讨论】:

    【解决方案3】:

    如果您愿意,也可以尝试使用此代码自定义其他内容

    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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 2012-06-04
      • 2014-03-29
      • 2013-01-07
      相关资源
      最近更新 更多