All of My UIalertcontroller Messages Became Single Line

All of my UIAlertController messages became single line

Do not know why the downvotes but I have found the issue after 3 days of pulling my hair out. There was a UILabel extension in one of the 3rd party Pods I have been using. Its code was marked as "Locked" so I think that's why it did not show up in the search results before. I had to download the source codes for all of the pods from their repositories and searched inside. It was overriding these functions and properties of the default UILabel, instead of creating its own UILabel subclass;

override open func draw(_ rect: CGRect) {}
override open var intrinsicContentSize: CGSize {}

Since the UIAlertViewController uses UILabel to display the message, I have tried to comment out these lines and my issue went away. If anyone having the same issue, search for UILabel extensions in your code AND in your embedded libraries source codes.

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

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)

}

Every UIAlertController disappear automatically before user responds - since iOS 13

I've got exactly the same problem, and fixed it by holding the window in which the alert is being presented in a strong variable.

You can hold a window for presenting alerts in you AppDelegate, for example, and use it in your UIAlertController extension.

//In app delegate
let alertWindow: UIWindow = {
let win = UIWindow(frame: UIScreen.main.bounds)
win.windowLevel = UIWindow.Level.alert + 1
return win
}()

Then, in your extension:

public extension UIAlertController {
func show() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let vc = UIViewController()
vc.view.backgroundColor = .clear
vc.view.tintColor = Theme.mainAccentColor
appDelegate.alertWindow.rootViewController = vc
appDelegate.alertWindow.makeKeyAndVisible()
vc.present(self, animated: true, completion: nil)
}
}

You will also need to make sure your alert window is removed from view when your alert is dismissed, otherwise your app will become unresponsive, as all taps will be handled by the (invisible) alert window, that's still on top of everything.
I do this by adding this code to the handlers of all actions in the alert:

(UIApplication.shared.delegate as! AppDelegate).alertWindow.isHidden = true

Open second UIAlertController from first UIAlertController

You had two errors,

first one, you need to present your second alert in UIAlertController name not in UIAlertAction name

 self.present(secondAlert, animated: true)

not method name alert2

self.present(alert2, animated: true)

second one, you need to call the first alertcontroller UIAlertAction
completion handler method like alert2 not alert2()

 firstAlert.addAction(UIAlertAction(title: "Option A", style: .default, handler: alert2 ))

full answer

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

let firstAlert = UIAlertController(title: "Title", message: "some message", preferredStyle: .alert)
firstAlert.addAction(UIAlertAction(title: "Option A", style: .default, handler: alert2 ))
firstAlert.addAction(UIAlertAction(title: "Option B", style: .default, handler: nil))
firstAlert.addAction(UIAlertAction(title: "Option C", style: .default, handler: nil))
firstAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

self.present(firstAlert, animated: true)
}

func alert2(alert: UIAlertAction!) {
//Put second alert code here:
let secondAlert = UIAlertController(title: "Title", message: "some message", preferredStyle: .alert)
secondAlert.addAction(UIAlertAction(title: "Option A", style: .default, handler: nil ))
secondAlert.addAction(UIAlertAction(title: "Option B", style: .default, handler: nil))
secondAlert.addAction(UIAlertAction(title: "Option C", style: .default, handler: nil))
secondAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(secondAlert, animated: true)
}

iOS 10: UIAlertController only showing one action

Under iOS 10, showing a UIAlertController with a style of "ActionSheet" doesn't work properly if you attempt to set the alert controller view's tintColor. I ran into this issue when updating my app for iOS 10. I filed a bug report with Apple.

So your primary issue will be solved by not calling:

[actionSheet.view setTintColor:[UIColor blackColor]];

under iOS 10.

Unrelated to this problem, your code is mistakenly attempting to dismiss the alert controller from inside the various alert actions. Do not do this. The alert will be dismissed automatically. You need to remove all of your calls to:

[actionSheet dismissViewControllerAnimated:YES completion:nil];

Multiline editable text UITextview inside UIAlertController?

This is good appraoch ...

func popUpController()
{

let alertController = UIAlertController(title: "\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertController.Style.actionSheet)

let margin:CGFloat = 8.0
let rect = CGRect(x: margin, y: margin, width: alertController.view.bounds.size.width - margin * 4.0, height: 100.0)
let customView = UITextView(frame: rect)

customView.backgroundColor = UIColor.clear
customView.font = UIFont(name: "Helvetica", size: 15)

// customView.backgroundColor = UIColor.greenColor()
alertController.view.addSubview(customView)

let somethingAction = UIAlertAction(title: "Something", style: UIAlertAction.Style.default, handler: {(alert: UIAlertAction!) in print("something")

print(customView.text)

})

let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: {(alert: UIAlertAction!) in print("cancel")})

alertController.addAction(somethingAction)
alertController.addAction(cancelAction)

self.present(alertController, animated: true, completion:{})

}


Related Topics



Leave a reply



Submit