我正在以编程方式创建一个 UITextField
,在我的 viewDidLoad
中称为 nameTextField
,如下所示:
var nameTextField = UITextField()
nameTextField = UITextField(frame: CGRect(x: 10, y: 20, width: 30, height: 20))
nameTextField.borderStyle = .roundedRect
nameTextField.autocorrectionType = UITextAutocorrectionType.no
nameTextField.returnKeyType = UIReturnKeyType.done
nameTextField.keyboardType = UIKeyboardType.default
nameTextField.backgroundColor = UIColor.white
nameTextField.placeholder = "Enter Your Full Name"
nameTextField.delegate = self
self.view.addSubview(nameTextField)
我不明白这是我的观点,我犯了什么错误?提前致谢。
最佳答案
可能是你的 View 背景颜色是白色
所以它不会显示改变背景颜色的原因
nameTextField.backgroundColor = UIColor.red
选择 2
如果你的VC嵌入了navigation controller,改变Y frame
并检查
nameTextField = UITextField(frame: CGRect(x: 10, y: 90, width: 30, height: 20))
以及改变背景颜色
nameTextField.backgroundColor = UIColor.red
关于ios - UITextField 未显示在 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42760222/