How to Insert the UITextView into UIAlertview in iOS

This should work. Give it a shot. But it’s a hack and might not be appreciated by Apple. UIAlertView *testAlert = [[UIAlertView alloc] initWithTitle:title message:@”” delegate:self cancelButtonTitle:@”Cancel” otherButtonTitles:@”Done”, nil]; UITextView *textView = [UITextView new]; if (SYSTEM_VERSION_LESS_THAN(@”7.0″))//For Backward compatibility { [testAlert addSubview: textView]; } else { [testAlert setValue: textView forKey:@”accessoryView”]; } [testAlert show];

How can I show alertview with activity indicator?

NOTE: This solution won’t work on iOS 7 and above. This is my take on it: alertView = [[UIAlertView alloc] initWithTitle:@”Submitting Entry” message:@”\n” delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn’t blur [alertView addSubview:spinner]; [spinner startAnimating]; [alertView show]; and dismiss in code using: [alertView dismissWithClickedButtonIndex:0 … Read more

How to dismiss UIAlertController when tap outside the UIAlertController?

Add a separate cancel action with style UIAlertActionStyleCancel. So that when user taps outside, you would get the callback. Obj-c UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@”Alert Title” message:@”A Message” preferredStyle:UIAlertControllerStyleActionSheet]; [alertController addAction:[UIAlertAction actionWithTitle:@”Cancel” style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { // Called when user taps outside }]]; Swift 5.0 let alertController = UIAlertController(title: “Alert Title”, message: “A Message”, preferredStyle: … Read more

AlertController is not in the window hierarchy

If you’re instancing your UIAlertController from a modal controller, you need to do it in viewDidAppear, not in viewDidLoad or you’ll get an error. Here’s my code (Swift 4): override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) let alertController = UIAlertController(title: “Foo”, message: “Bar”, preferredStyle: .alert) alertController.addAction(UIAlertAction(title: “OK”, style: .cancel, handler: nil)) present(alertController, animated: true, completion: … Read more

Is it possible to show an Image in UIAlertView?

You can do it like: UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@”OK” otherButtonTitles:nil]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 10, 40, 40)]; NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@”smile.png”]]; UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path]; [imageView setImage:bkgImg]; [successAlert addSubview:imageView]; [successAlert show]; This will add a image in the right corner of … Read more

UIAlertView/UIAlertController iOS 7 and iOS 8 compatibility

The detection pattern is identical to the Objective-C style. You need to detect whether the current active runtime has the ability to instantiate this class if objc_getClass(“UIAlertController”) != nil { println(“UIAlertController can be instantiated”) //make and use a UIAlertController } else { println(“UIAlertController can NOT be instantiated”) //make and use a UIAlertView } Don’t try … Read more

Custom Alert (UIAlertView) with swift

Code tested in Swift 5 and Xcode 10 How to make your own custom Alert I was wanting to do something similar. First of all, UIAlertView is deprecated in favor of UIAlertController. See this answer for the standard way to display an alert: How would I create a UIAlertView in Swift? And both UIAlertView and … 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

How to add subview inside UIAlertView for iOS 7?

You can really change accessoryView to customContentView in iOS7 (and it seems that in iOS8 as well) UIAlertView [alertView setValue:customContentView forKey:@”accessoryView”]; Try this code: UIAlertView *av = [[UIAlertView alloc] initWithTitle:@”TEST” message:@”subview” delegate:nil cancelButtonTitle:@”NO” otherButtonTitles:@”YES”, nil]; UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)]; [av setValue:v forKey:@”accessoryView”]; v.backgroundColor = [UIColor yellowColor]; [av show]; Remember that … Read more