Ios swift我想更改搜索栏背景颜色白色带黑色边框

Ios swift我想更改搜索栏背景颜色白色带黑色边框,ios,swift,Ios,Swift,我想要的结果像这个搜索栏与背景色白色和黑色边框 这是我的密码 override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(true) for subView in searchBar.subviews { if !subView.subviews.contains(where: { $0 as? UITextField != nil }) { continue }

我想要的结果像这个搜索栏与背景色白色和黑色边框 这是我的密码

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    
    for subView in searchBar.subviews  {
        if !subView.subviews.contains(where: { $0 as? UITextField != nil }) { continue }
        guard let textField = subView.subviews.first(where: { $0 as? UITextField != nil }) as? UITextField else { return }
        
        let placeholder = NSMutableAttributedString(
            string: "Search",
            attributes: [.font: UIFont(name: "Helvetica", size: 15.0)!,
                         .foregroundColor: UIColor.gray
            ])
        textField.attributedPlaceholder = placeholder
        textField.layer.cornerRadius = textField.frame.size.height / 2
        textField.layer.masksToBounds = true
        textField.textColor = .black
        textField.backgroundColor = .white
        
    }


我无法设置搜索栏,请建议我早安!如果我理解正确,那么这可能会解决你的问题。UISearchBar的扩展(Swift 5):

此外,您可以尝试使用图像来执行此操作。 仅使用Apple API进行此操作的方法是创建一个映像并使用setSearchFieldBackgroundImage:

self.searchBar.setSearchFieldBackgroundImage(UIImage(named: "SearchFieldBackground"), for: UIControlState.normal)

来源:

它向我显示错误//类型“NSAttributeString”没有成员“Key”。在这一行上-textField.attributedPlaceholder=NSAttributedString(字符串:textField.placeholder??),属性:[NSAttributedString.Key.foregroundColor:placeholder Text])在上下文中显示您尝试使用NSAttributedString.Key的实际代码
self.searchBar.setSearchFieldBackgroundImage(UIImage(named: "SearchFieldBackground"), for: UIControlState.normal)