UIAlertController with textfield compressed in landscape mode

113 Views Asked by At

image here

I found this problem in working project. After I've created the simplest UIAlertController with textfield in empty project and found same problem. Looks like system bug?

How to reproduce: Launch code below in portrait mode and then rotate to landcape.

I checked in simulators 15 Pro, 14 Pro, iOS 17/16. Everything is fine on iPhone SE.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let alertController = UIAlertController(title: "Alert Title", message: "This is a sample alert.", preferredStyle: .alert)
    
    alertController.addTextField { (textField) in
        textField.placeholder = "Additional Text"
    }
    
    let okAction = UIAlertAction(title: "OK", style: .default) { (action) in
        if let textField = alertController.textFields?.first {
            print("Additional Text: \(textField.text ?? "")")
        }
    }
    alertController.addAction(okAction)
    
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    alertController.addAction(cancelAction)
    
    present(alertController, animated: true, completion: nil)
}
2

There are 2 best solutions below

4
Mohammad Reza Ansari On

No, it's not a system bug. It seems like you might be encountering issues related to a third-party library. I recommend using the native iOS built-in feature UIAlertController for displaying alerts, as shown in the provided code example:

@objc func showAlert() {
    let alertController = UIAlertController(title: "Alert Title", message: "This is a sample alert.", preferredStyle: .alert)

    // Add a text field to the alert
    alertController.addTextField { (textField) in
    textField.placeholder = "Additional Text"
    }

    let okAction = UIAlertAction(title: "OK", style: .default) { (action) in
        // Access the text field's text here if needed
        if let textField = alertController.textFields?.first {
            print("Additional Text: \(textField.text ?? "")")
        }
    }
    alertController.addAction(okAction)

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    alertController.addAction(cancelAction)

    present(alertController, animated: true, completion: nil)
}


// Override to allow rotation to landscape mode
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .all
}

// Override to specify the initial interface orientation
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .landscapeLeft
}

enter image description here

If the issue persists, it would be helpful to see more code or receive additional information about the problem you are facing.

0
Danil N. On

I looked at system applications and on such screens they do not allow rotate to landscape. So i think it's good solution:

extension UIAlertController {
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        textFields?.isEmpty == true ? .all : .portrait
    }
}