为什么 UITextField 不允许我添加边框颜色?

Why is UITextField not allowing me to add border color?

我有下面的方法,应该向 uitextfield 添加边框颜色,但由于某种原因,它没有添加边框颜色,尽管它确实成功地添加到可以输入的视图中。

func setupSearchBar() {
    searchField.frame = CGRect(x: 4, y: 6.5, width: 366.58, height: 42)
    searchField.layer.cornerRadius = searchField.frame.height/2
    topBackgroundView.layer.borderWidth = 0.35
    searchField.layer.borderColor = UIColor(red: 0/225, green: 0/225, blue: 0/225, alpha: 0.49).cgColor
    searchField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
    view.addSubview(searchField)
}

搜索字段 borderWidth 默认为 0。试试:

func setupSearchBar() {
    searchField.frame = CGRect(x: 4, y: 6.5, width: 366.58, height: 42)
    searchField.layer.cornerRadius = searchField.frame.height/2
    topBackgroundView.layer.borderWidth = 0.35
    searchField.layer.borderColor = UIColor(red: 0/225, green: 0/225, blue: 0/225, alpha: 0.49).cgColor
    searchField.layer.borderWidth = 1.0 // <- Add here
    searchField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
    view.addSubview(searchField)
}