在Swift中指定UITextField的边框半径

Dan*_*all 31 uitextfield ios swift

我想以编程方式指定UITextField使用Swift 的边界半径.

默认情况下,a UITextField有一个轻微的边界半径,但是我想增加它,并且在通过Interface Builder进行拖网后,我认为这可以通过编程方式完成?

Fre*_*Loh 62

您可以使用:

nameOfTextField.layer.cornerRadius = 15.0
nameOfTextField.layer.borderWidth = 2.0
nameOfTextField.layer.borderColor = UIColor.red.cgColor
Run Code Online (Sandbox Code Playgroud)


Jas*_*ell 19

如果您正在使用Interface Builder,则可以将"用户定义的运行时属性"用于要修改的控件.在运行时,当视图加载时,您传递键路径的每个属性将自动设置为您的值.只需很少的显示调整就不需要弄乱你的代码.

下面的图片应该回答你的问题.

使用IB运行时属性添加边界半径

  • 这是最好的方法,因为您不允许UI代码存在于Controller中. (3认同)

小智 8

要在答案下方创建文本字段半径是正确的:

迅捷3

YourTextfieldName.layer.cornerRadius = YourTextfieldName.frame.size.height/2
YourTextfieldName.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)


Fai*_*san 5

在 Swift 5 中

yourTxtField.layer.cornerRadius = 18
yourTxtField.layer.masksToBounds = true
Run Code Online (Sandbox Code Playgroud)