Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize UIAlertController in iOS 8 to include standard elements like UITableView

I am used to customize UIAlertViews through the [alert setValue:someView forKey:@"accessoryView"] method. This creates customizable content for UIAlertViews with custom heights. However it only works on iOS7 and down. In iOS8 the UIAlertController have taken over, and I cannot customize it anymore, it will cut the height of the UIAlertView.

Is it impossible because of misuse of the UIAlertController, or how am I supposed to do it? I am trying to incorporate a UITableView inside a UIAlertController with UIAlertControllerStyleAlert.

Thx.

like image 357
kbjeppesen Avatar asked Sep 17 '14 17:09

kbjeppesen


3 Answers

I ran into the same issue right now. I looked at the private header for UIAlertController (https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UIAlertController.h) and found a promising property: contentViewController

And it turned out to be exactly the same as accessoryView used to be for UIAlertView, the difference being that you need to assign a UIViewController to this property rather than a UIView.

UIViewController *v = [[UIViewController alloc] init];
v.view.backgroundColor = [UIColor redColor];

[alertController setValue:v forKey:@"contentViewController"];

That piece of code will show a red view on the alert view! Happy UIAlertController customizing ;)

PS. It is a private property but using KVC there shouldn't be a problem App Store wise, I think.

Edit:

Some people complained that this isn't very safe. It's not a public API, so yes, Apple could change it in any release, causing this method to fail.

To make sure your entire app doesn't crash if that happens you could wrap the KVC call in a try block. If the property changes your controller won't show the content view, but it also won't crash:

@try {
    [alertController setValue:v forKey:@"contentViewController"];
}
@catch(NSException *exception) {
    NSLog(@"Failed setting content view controller: %@", exception);
}

Using this method in production can be risky, and I don't recommend it for important alerts.

like image 52
JonasG Avatar answered Oct 22 '22 00:10

JonasG


I suggest not your wasting time trying to cram additional UI into a place where isn't supposed to be. Based on the last few years of improvements, Apple will probably add a custom view in the next iOS. Until then, have a look at a framework designed to handle this exact situation without subverting any best practices: SDCAlertView

It supports alerts that imitate the native alerts on iOS 7,8,9, including handling all of the nasty edge cases around sizing, button types, rotation, etc. It does support arbitrary custom views within the alert.

SDCAlertView demo gif

I use this library in Yahoo YMPromptKit for custom push notification prompts that look exactly like iOS native. Here's another example:

enter image description here

like image 35
Adam Kaplan Avatar answered Oct 22 '22 01:10

Adam Kaplan


I think you can easily customize the UIView adding the controls needed and present it modally, unless you have any other specific reason to use only UIAlertController.

https://www.cocoacontrols.com/search?q=UIAlertview

like image 26
CyberInfo Avatar answered Oct 21 '22 23:10

CyberInfo