在 Swift 中,我為文本欄位添加了邊框顏色紅色。我想實作的是,如果我們在其中輸入任何文本,則應洗掉文本欄位邊框顏色,如果再次沒有文本,則應再次將邊框顏色設定為紅色。無需單擊任何按鈕。(僅以編程方式)
uj5u.com熱心網友回復:
你可以用下面的簡單代碼來做到這一點。在您的 viewdidload 或查看配置方法中添加以下行
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
根據需要更改文本欄位邊框顏色或洗掉邊框
@objc func textFieldDidChange(_ textField: UITextField) {
if (textField.text!.isEmpty)
{
// change your textfield border color
}
else
{
// remove text field border or change color
}
}
uj5u.com熱心網友回復:
這應該有幫助:
if self.yourTextField.text.isEmpty
{
self.yourTextField.layer.borderColor = UIColor.red.cgColor
self.yourTextField.layer.borderWidth = 1
}
else
{
self.yourTextField.layer.borderWidth = 0
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/357357.html
