gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:24:53 25 4
gpt4 key购买 nike

出于某种原因,我想要指定 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com