首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法在UISearchController中为UITextField设置正确的背景色

无法在UISearchController中为UITextField设置正确的背景色
EN

Stack Overflow用户
提问于 2018-09-17 16:20:07
回答 3查看 635关注 0票数 0

我使用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的代码

EN

回答 3

Stack Overflow用户

发布于 2018-09-17 16:25:24

在UIColor扩展中创建类属性

代码语言:javascript
运行
复制
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

票数 0
EN

Stack Overflow用户

发布于 2018-09-17 19:30:20

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

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

代码语言:javascript
运行
复制
controller.searchBar.tintColor = Color.theme.value
票数 0
EN

Stack Overflow用户

发布于 2018-09-17 20:50:45

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

代码语言:javascript
运行
复制
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)
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52363502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档