The test field was originally centered. However, as I tap on it, it was brought to the left. This UI issue occurs on macOS 13.2, built on My Mac(Designed for iPad), while running on iPad is totally fine. Xcode 14.2 and 14.1 has the same behaviour.
import UIKit
class ViewController: UIViewController {
let textField: UITextField = {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.backgroundColor = .green
textField.textAlignment = .center
textField.text = "Hello, World"
return textField
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(textField)
NSLayoutConstraint.activate([
textField.widthAnchor.constraint(equalToConstant: 200),
textField.heightAnchor.constraint(equalToConstant: 50),
textField.centerXAnchor.constraint(equalTo: view.centerXAnchor),
textField.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
}
How can I fix this?