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.