I would like to implement a UIAlertViewController
like this (Reference: Pages and Keynote app):
.
I have implemented a custom tableview and pres
UIAlertController
has a private contentViewController
property which allows you to set any UIViewController
subclass as part of the alert controller. It works well with both action sheet or alert styles. You can mix content view controller with other alert actions.
This is how UIActivityViewController
, the Airdrop preview controller, etc. are implemented.
Best practice is to subclass UIAlertController
, and in either initWithNibName:bundle:
or viewDidLoad
, use setValue:forKey:
to set the contentViewController
property. Don't forget to set the preferredContentSize
for your content view controller.
Adding on to Leo's answer, yes, there is a private property on UIAlertController
contentViewController
which allows you to set a UIViewController
(and it's view) as the content of the UIAlertController
.
You can create a private interface to access this property without using KVO or importing a private header like so:
@interface UIAlertController (ContentViewController)
@property (nonatomic, strong) UIViewController * contentViewController;
@end
Then, layout your custom view in your content view controller's view
, via Interface Builder or programmatically.
Remember that you also need to override your view controller's preferredContentSize
:
- (CGSize)preferredContentSize {
CGSize contentSize = [super preferredContentSize]; //gets the preferredContentHeight from the view, will be set depending on how much content we have
return CGSizeMake(contentSize.width, self.view.preferredContentHeight);
}
Note: rather than overriding the getter, Leo Natan suggests setting the preferredContentSize
directly, since it is a property on UIViewController
.
You can also override your view controller's view
in your subclass as well, if you want.
Set up your alert as your normally would:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message"
preferredStyle:UIAlertControllerStyleActionSheet];
Set your custom view:
[alertController setContentViewController:[[MyContentViewController alloc] init]];
Add your actions:
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^{
NSLog(@"Cancel Button was pressed");
}];
Present as your normally would:
[self presentViewController:alertController animated:YES completion:nil];
More information on the private APIs of UIAlertController
can be found on the iPhone Dev Wiki's article on UIAlertController.
I would also check out the private interfaces on _UIAlertControllerActionView and UIAlertAction, as well as the UIAlertActionViewRepresentation_Internal protocol.
I think you should use UIAlertController
with a preferredStyle
set to UIAlertControllerStyleActionSheet
.
But if this is not enough start from scratch, from UIWindow
and UIViewController
with tableView and make any UI you want, there are a lot of custom alertView implementations I'm sure you easily found one for example.