Search code examples
iosswift3uitextfieldxcode8.2uialertviewcontroller

All the alert dialog message and textField have been changed to single line. Please checkout the image


Previously all the dialog and textField are working well. But not I do not know how these TextFields are suddenly changed to single line with triple. (Like some Message here...)

    let alert = UIAlertController(title: "Cancel Booking !!", message: "Are you sure you want to cancel your booking?", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "No", style: .default, handler: nil))
    alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: self.cancelMessageDialog))
    self.present(alert, animated: true, completion: nil)

Demo 1 Demo 3


Solution

  • Solved Finally

    I solved this problem by making the custom UILable inside the UIViewController. This may not be the good practice, so kindly let me know if someone find the better solution then this.

    func showTestAlert(message:String , viewController:UIViewController){
    
        let customUiLableView:UILabel
        let alert:UIAlertController
    
        if((message.count) < 100){
            alert = UIAlertController(title: "", message: "\n\n\n\n", preferredStyle: .alert)
            customUiLableView = UILabel(frame: CGRect(x: 10, y: 0, width: 250, height: 120))
            customUiLableView.numberOfLines = 4
        }else if((message.count) < 200){
            alert = UIAlertController(title: "", message: "\n\n\n\n\n\n", preferredStyle: .alert)
            customUiLableView = UILabel(frame: CGRect(x: 10, y: 0, width: 250, height: 160))
            customUiLableView.numberOfLines = 6
        }else{
            alert = UIAlertController(title: "", message: "\n\n\n\n\n\n\n\n", preferredStyle: .alert)
            customUiLableView = UILabel(frame: CGRect(x: 10, y: 0, width: 250, height: 200))
            customUiLableView.numberOfLines = 8
        }
        customUiLableView.text = message
        customUiLableView.textAlignment = .center
        customUiLableView.textColor = UIColor.darkGray
        customUiLableView.font = UIFont(name: "Helvetica", size: 16.0)
    
        let action = UIAlertAction(title: "OK", style: .default, handler: nil)
        alert.view.addSubview(customUiLableView)
        alert.addAction(action)
    
        viewController.present(alert, animated: true, completion: nil)
    
    }