我目前正在试验 UITextfields(我是初学者)。 现在我已经设置了我的应用程序的背景颜色并设置了边框半径,但是在执行此操作时我遇到了一个小问题。
我的代码:
superView.backgroundColor = backgroundColorrr
loginView.backgroundColor = backgroundColorrr
loginLabel.textColor = UIColor.white
loginLabel.text = "Login"
loginButton.layer.cornerRadius = 5
loginButton.layer.borderWidth = 2.0
loginButton.imageView?.contentMode = UIViewContentMode.scaleToFill
loginButton.layer.borderColor = UIColor.lightGray.cgColor
loginButton.layer.backgroundColor = backgroundColorrr.cgColor
loginButton.titleLabel?.textColor = UIColor.white
loginButton.titleLabel?.text = "Login"
userNameTextField.layer.cornerRadius = 15.0
userNameTextField.layer.backgroundColor = backgroundColorrr.cgColor
userNameTextField.layer.borderWidth = 0.5
我的结果:
你看我想要实现的是与屏幕的其余部分具有相同的背景颜色(我不希望文本字段的边框旁边是白色。我的按钮也是如此,边框半径取胜' 完全改变(使用的图像与我的按钮大小相同。
欢迎任何帮助!
最佳答案
如果您想应用cornerRadius
,您需要设置layer.masksToBounds = true
。
设置
userNameTextField.layer.masksToBounds = true
和 loginButton.layer.masksToBounds = true
您的完整代码应如下所示:
superView.backgroundColor = backgroundColorrr
loginView.backgroundColor = backgroundColorrr
loginLabel.textColor = UIColor.white
loginLabel.text = "Login"
loginButton.layer.masksToBounds = true
loginButton.layer.cornerRadius = 5
loginButton.layer.borderWidth = 2.0
loginButton.imageView?.contentMode = UIViewContentMode.scaleToFill
loginButton.layer.borderColor = UIColor.lightGray.cgColor
loginButton.layer.backgroundColor = backgroundColorrr.cgColor
loginButton.titleLabel?.textColor = UIColor.white
loginButton.titleLabel?.text = "Login"
userNameTextField.layer.masksToBounds = true
userNameTextField.layer.cornerRadius = 15.0
userNameTextField.layer.backgroundColor = backgroundColorrr.cgColor
userNameTextField.layer.borderWidth = 0.5
注意:如果您应用 clipsToBounds = true
,您可以在 cornerRadius
上获得“相同”的结果。
关于ios - Swift 4 - UITextfield 边框半径颜色问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47367483/