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

时间:2019-06-18 22:06:22

标签: ios swift uitextfield

我有以下方法,应该将边框颜色添加到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)
}

1 个答案:

答案 0 :(得分:4)

搜索字段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)
}