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

标签 ios swift uitextfield

我有下面的方法,它应该向 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/

相关文章:

swift - 相等运算符是否意味着强制展开?

ios - 实现自动完成文本字段 Objective C

objective-c - 如何在 iPad 的 UITextField 中限制特殊字符?

ios - UITextField 代表不工作

ios - facebook 登录错误,react-native ios expo

ios - react native (ios)中的应用程序仅在手机连接到 mac 时启动

iphone - 如何验证文本字段中只允许单个小数点的文本字段?

iphone - 设置 UITabBarControllers 的 selectedIndex 属性后禁用自动旋转(SDK 错误?)

ios - 为什么我的游戏中的音频没有记录在 ReplayKit 中?

swift - 在swift中子类化专门的泛型类