How to customize UIAlertController with UITableView or is there any default control available like this UI in Pages app?

后端 未结 3 881
不知归路
不知归路 2020-12-29 09:50

I would like to implement a UIAlertViewController like this (Reference: Pages and Keynote app):

.

I have implemented a custom tableview and pres

相关标签:
3条回答
  • 2020-12-29 10:06

    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.

    0 讨论(0)
  • 2020-12-29 10:09

    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.

    0 讨论(0)
  • 2020-12-29 10:24

    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.

    0 讨论(0)
提交回复
热议问题