swift - UITextField 边框在 Swift 中指定半径

标签 swift xcode uitextfield border radius

出于某种原因,我想要指定 30 的圆角半径不会显示在我的 XcodeSwift 中编码的主 Storyboard中。

如果我使用 passWordTextField.borderStyle = UITextField.BorderStyle.roundedRect(这将不允许我指定角 radius/borderstyle 值),文本字段将出现,但是如果我使用 passWordTextField.borderStyle = UITextField.BorderStyle.init(rawValue: 30)! 则不会。

func customTextField (xco: CGFloat, yco: CGFloat, widthLength: CGFloat, heightLength: CGFloat, printSize: CGFloat) {

    let passWordTextField =  UITextField(frame: CGRect(x: xco, y: yco, width: widthLength, height: heightLength ))
    let fontSize = CGFloat (printSize)
    passWordTextField.placeholder = "Enter text here" //set placeholder text
    passWordTextField.font = UIFont.systemFont(ofSize: fontSize) // set font size of text field

    //passWordTextField.borderStyle = UITextField.BorderStyle.init(rawValue: 30)!
    passWordTextField.layer.cornerRadius = 30.0
   // passWordTextField.borderStyle = UITextField.BorderStyle.roundedRect
    passWordTextField.autocorrectionType = UITextAutocorrectionType.no
    passWordTextField.keyboardType = UIKeyboardType.default
    passWordTextField.returnKeyType = UIReturnKeyType.done
    passWordTextField.clearButtonMode = UITextField.ViewMode.whileEditing
    passWordTextField.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
    passWordTextField.delegate = self as? UITextFieldDelegate
    self.view.addSubview(passWordTextField)

最佳答案

如果您也想在 Storyboard 上看到您的更改,您可以使用自定义文本字段类。

public class CustomTextField: UITextField {
    public override init(frame: CGRect) {
      super.init(frame: frame)
      prepare()
    }

    required init?(coder decoder: NSCoder) {
      super.init(coder: decoder)
      prepare()

    private func prepare() {
       layer.cornerRadius = 30
    }
}

现在,在您的 Storyboard 上,您可以将身份检查器中的文本字段类更改为您的 CustomTextField。

关于swift - UITextField 边框在 Swift 中指定半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58555115/

相关文章:

ios - 如何在键盘隐藏上为 UITableView 设置动画?

ios - 多个获取请求?

ios - Xcode 5 自动布局 - 嵌入式表格

ios - 既然有 Storyboard,那么使用多个 nib 文件是否有意义?

objective-c - 实现弃用的方法 - CLLocation

objective-c - 在 Textfield 中,在 ios 5 中完美工作时,无法使用 ios 6 和 xcode 4.5 键入任何内容

ios - UICollectionView 不重新加载数据

swift - 未加载 uicollectionview?

ios - 如何将图像保存到 Parse?

iphone - 如何禁用 UITextField 或键盘的文本自动更正?