0

For some reason my corner radius that I want specified 30 will not display in my mainstoryboard for Xcode coding in Swift.

The textfield will appear if I use passWordTextField.borderStyle = UITextField.BorderStyle.roundedRect (this will not allow me to specify a corner radius/borderstyle value), but will not if I use 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)

2 Answers 2

1

If you want to see your changes on a Storyboard as well you can use a custom textfield class.

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

Now on your Storyboard you are able to change the class of your textfield in the Identity inspector to your CustomTextField.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to set @IBDesignable and prepareForInterfaceBuilder() to reflect your textField in the Interface Builder. Here are the steps: 1. make a subclass of UITextField like below

import UIKit
@IBDesignable
class CostumTxtField: UITextField {
    override func prepareForInterfaceBuilder() {
        makeCorenerRadius()
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        makeCorenerRadius()

    }


 override init(frame: CGRect) {
      super.init(frame: frame)

    autocorrectionType = .no
    keyboardType = .default
    returnKeyType = .done
    clearButtonMode = .whileEditing
    contentVerticalAlignment = .center
    makeCorenerRadius()

    }

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

    func makeCorenerRadius(){
        backgroundColor = .white
        layer.cornerRadius = 20

}
}

  1. In your view controller edit your function like follows:
func customTextField (xco: CGFloat, yco: CGFloat, widthLength: CGFloat, heightLength: CGFloat, fontSize: CGFloat) {

    let passWordTextField =  UITextField(frame: CGRect(x: xco, y: yco, width: widthLength, height: heightLength )) {

    passWordTextField.placeholder = "Enter text here" //set placeholder text
    passWordTextField.font = UIFont.systemFont(ofSize: fontSize) // set font size of text field

    passWordTextField.delegate = self as? UITextFieldDelegate
    self.view.addSubview(passWordTextField)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.