iOS UIAlertController bold button changed in 8.3

From iOS 9 you can set the preferredAction value to the action which you want the button title to be bold. let cancelAction = UIAlertAction(title: “Cancel”, style: .Cancel, handler: nil) let OKAction = UIAlertAction(title: “OK”, style: .Default, handler: nil) alert.addAction(cancelAction) alert.addAction(OKAction) alert.preferredAction = OKAction presentViewController(alert, animated: true) {} The OK button which is on the … Read more

How to dismiss UIAlertController when tap outside the UIAlertController?

If you are targeting devices having iOS > 9.3 and using Swift and preferredStyle is Alert you can use snippet as below: func showAlertBtnClicked(sender: UIButton) { let alert = UIAlertController(title: “This is title”, message: “This is message”, preferredStyle: .Alert) self.presentViewController(alert, animated: true, completion:{ alert.view.superview?.userInteractionEnabled = true alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertControllerBackgroundTapped))) }) } func alertControllerBackgroundTapped() { … Read more

Add Image to UIAlertAction in UIAlertController

And that’s how it’s done: let image = UIImage(named: “myImage”) var action = UIAlertAction(title: “title”, style: .default, handler: nil) action.setValue(image, forKey: “image”) alert.addAction(action) the image property is not exposed, so there’s no guarantee of this working in future releases, but works fine as of now

How to add TextField to UIAlertController in Swift

Swift 5.1 alert.addTextField { (textField) in textField.placeholder = “Enter First Name” } Use this code, I am running this code in my app successfully. @IBAction func addButtonClicked(sender : AnyObject){ let alertController = UIAlertController(title: “Add New Name”, message: “”, preferredStyle: UIAlertControllerStyle.Alert) alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in textField.placeholder = “Enter Second Name” } let … Read more

Writing handler for UIAlertAction

Instead of self in your handler, put (alert: UIAlertAction!). This should make your code look like this alert.addAction(UIAlertAction(title: “Okay”, style: UIAlertActionStyle.Default, handler: {(alert: UIAlertAction!) in println(“Foo”)})) this is the proper way to define handlers in Swift. As Brian pointed out below, there are also easier ways to define these handlers. Using his methods is discussed … Read more

UIAlertView first deprecated IOS 9

From iOS8 Apple provide new UIAlertController class which you can use instead of UIAlertView which is now deprecated, it is also stated in deprecation message: UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead So you should use something like this UIAlertController * alert = [UIAlertController alertControllerWithTitle:@”Title” message:@”Message” preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* yesButton = [UIAlertAction … Read more

What is the best way to check if a UIAlertController is already presenting?

It is not the UIAlertController that is “already presenting”, it is MessagesMasterVC. A view controller can only present one other view controller at a time. Hence the error message. In other words, if you have told a view controller to presentViewController:…, you cannot do that again until the presented view controller has been dismissed. You … Read more

UIAlertController custom font, size, color

Not sure if this is against private APIs/properties but using KVC works for me on ios8 UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@”Dont care what goes here, since we’re about to change below” message:@”” preferredStyle:UIAlertControllerStyleActionSheet]; NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@”Presenting the great… Hulk Hogan!”]; [hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50.0] range:NSMakeRange(24, 11)]; [alertVC setValue:hogan forKey:@”attributedTitle”]; UIAlertAction *button = … Read more

Get input value from TextField in iOS alert in Swift

Updated for Swift 3 and above: //1. Create the alert controller. let alert = UIAlertController(title: “Some Title”, message: “Enter a text”, preferredStyle: .alert) //2. Add the text field. You can configure it however you need. alert.addTextField { (textField) in textField.text = “Some default text” } // 3. Grab the value from the text field, and … Read more

tech