UITextField边框在Swift中指定半径

问题描述 投票:0回答:1

出于某种原因,我想要指定为“ 30”的拐角半径不会在Swift的Xcode编码的主板上显示。如果我使用//passWordTextField.borderStyle = UITextField.BorderStyle.roundedRect [这将不允许我指定转角半径/边界样式值],则文本字段将出现,但如果我使用//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)
swift xcode uitextfield border radius
1个回答
1
投票

如果您还想在情节提要上查看所做的更改,则可以使用自定义文本字段类。

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
    }
}

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

© www.soinside.com 2019 - 2024. All rights reserved.