UITextField's text disappeared when focused.

My view has several textfields for register.
The problem is when I focus on a textfield to update, the text and the caret in the textfield disappeared (but it still stores the value). The text value only appears after the textfield lost keyboard focus.

This error happen with all the devices I have (iPhone 12, iPad), all the simulators I tried. The only thing that works fine is when I using the simulator, I typed in textfield by my Mac physical keyboard instead of simulator's onscreen keyboard.

I'm new in iOS and Swift so please can you help me to resolve this. I could provide UI Hierarchy if needed
May be you have asked to clear when entering textField ? Check in IB.

And please, show code.
Thanks you, here my code for RegisterView

Code Block import SwiftUI
class RegisterView : UIView, UITextFieldDelegate {
    @IBOutlet weak var username : UITextField?
    @IBOutlet weak var password : UITextField?
    @IBOutlet weak var repassword : UITextField?
    @IBOutlet weak var email : UITextField?
    @IBOutlet weak var displayname : UITextField?
    var contentView : UIView?
    public static var nibName: String {
            return String(describing: self.self)
        }
    
    public static var nib: UINib {
            let bundle = Bundle(for: self.self)
            return UINib(nibName: self.nibName, bundle: bundle)
        }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupView()
    }
    required init?(coder : NSCoder) {
        super.init(coder: coder)
        setupView()
    }
    private func setupView () {
        guard let view = Self.nib.instantiate(withOwner: self, options: nil).first as? UIView else { fatalError("Error loading \(self) from nib") }
                addSubview(view)
                view.translatesAutoresizingMaskIntoConstraints = false
        if #available(iOS 11.0, *) {
            view.leadingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
            view.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
            view.trailingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
            view.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
        }
        self.displayname?.delegate = self
        self.email?.delegate = self
        self.username?.delegate = self
        self.password?.delegate = self
        self.repassword?.delegate = self
        super.layoutSubviews()
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return true
    }
    public func register(username: String, email : String, displayName : String, password : String) {
    }
}



And here's the code for add RegisterView into parent view (I'm planning to switch between RegisterView and LoginView in parent view)


Code Block @IBAction func register(_ sender: UIButton) {
        self.processView?.showTextOnly(text: "register button clicked")
        
        if (self.registerView != nil) {
            self.upperView.removeChildView()
            self.upperView.addSubview(registerView!)            
            return
        }
        self.registerView = RegisterView();
        self.registerView?.frame = upperView.bounds
        self.registerView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.upperView.addSubview(registerView!)
        self.registerView?.center = CGPoint(x : upperView.frame.width / 2,
                                      y : upperView.frame.height / 2)
    }



@tuanncong in this case I don't know if you have fixed the error ?, I am same problem

UITextField's text disappeared when focused.
 
 
Q