ios - Swift 4 - UITextfield 边框半径颜色问题

标签 ios swift uitextfield swift4 cornerradius

我目前正在试验 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

我的结果:

current layout in simulator

你看我想要实现的是与屏幕的其余部分具有相同的背景颜色(我不希望文本字段的边框旁边是白色。我的按钮也是如此,边框半径取胜' 完全改变(使用的图像与我的按钮大小相同。

欢迎任何帮助!

最佳答案

如果您想应用cornerRadius,您需要设置layer.masksToBounds = true

设置 userNameTextField.layer.masksToBounds = trueloginButton.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/

相关文章:

ios - 如何更改 CreationDate 资源值

ios - 我是否需要为我的每个应用程序申请证书才能在应用程序商店上发布?

ios - Xcode 6 归档并在将 "Skipping copy phase strip ,binary is code signed"添加到目标时收到警告 "share extension"

swift - 从解析服务器到本地 sqlite (Swift 3)

ios - 小数点不能用逗号进行数学运算。需要点

ios - 苹果手机 : How to capitalize the first character of my textfield?

Swift - 如何按时间间隔在 UITextfield 中保存输入的信息?

iphone - 在单元格内实现具有多个标签的 TableView

ios - 更少的 Storyboard序列和更多的代码是否会使应用程序更快/更小?

swift - 如何在Swift中使用animationDidStop