我有下面的方法,它应该向 uitextfield 添加边框颜色,但由于某种原因,它没有添加边框颜色,尽管它确实成功地添加到可以输入的 View 中。
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)
}
关于ios - 为什么 UITextField 不允许我添加边框颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56657646/