如何使用 UITableView 自定义 UIAlertController,或者 Pages 应用程序中是否有类似 UI 的默认控件可用?

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

我想实现这样的 UIAlertViewController(参考:Pages 和 Keynote 应用程序):

.

我实现了一个自定义表格视图,并通过模仿 UIAlertViewController 来呈现视图。但是我无法实现与上面类似的 UI 。是否有任何可用的默认控制器?

-(void)share:(id)sender
{
    [self setModalPresentationStyle:UIModalPresentationCurrentContext];

    AlertViewController *renameCntrl = [self.storyboard instantiateViewControllerWithIdentifier:AlertTblViewidentifier];
    renameCntrl.optionViewDelegate = self;
    renameCntrl.providesPresentationContextTransitionStyle = YES;
    renameCntrl.definesPresentationContext = YES;
    [renameCntrl setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    [self presentViewController:renameCntrl animated:YES completion:nil];
}

我认为你应该使用 UIAlertController 并将 preferredStyle 设置为 UIAlertControllerStyleActionSheet

但是,如果这还不够,从头开始,从 UIWindowUIViewController 开始,使用 tableView 并制作您想要的任何 UI,我有很多自定义 alertView 实现例如,我确定您很容易找到一个。

UIAlertController 有一个私有 contentViewController 属性 允许您将任何 UIViewController 子类设置为警报控制器的一部分。它适用于操作 sheet 或警报样式。您可以将内容视图控制器与其他警报操作混合使用。

这就是UIActivityViewController、Airdrop 预览控制器等的实现方式。

最佳做法是继承 UIAlertController,在 initWithNibName:bundle:viewDidLoad 中,使用 setValue:forKey: 设置 contentViewController 属性 .不要忘记为您的内容视图控制器设置 preferredContentSize

除了 Leo 的回答之外,是的,UIAlertController contentViewController 上有一个私人 属性,它允许您将 UIViewController(及其视图)设置为UIAlertController.

的内容

您可以创建一个私有接口来访问此 属性,而无需使用 KVO 或导入私有 header,如下所示:

@interface UIAlertController (ContentViewController)
@property (nonatomic, strong) UIViewController * contentViewController;
@end

然后,通过 Interface Builder 或以编程方式在您的内容视图控制器 view 中布置您的自定义视图。

请记住,您还需要覆盖视图控制器的 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); 
}

注意:Leo Natan 建议直接设置 preferredContentSize 而不是覆盖 getter,因为它是 UIViewController 上的 属性。

如果需要,您也可以在子类中覆盖视图控制器的 view

像往常一样设置提醒:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" 
                                                                                    preferredStyle:UIAlertControllerStyleActionSheet];

设置您的自定义视图:

[alertController setContentViewController:[[MyContentViewController alloc] init]];

添加您的操作:

[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^{
                       NSLog(@"Cancel Button was pressed");
}];

像往常一样呈现:

[self presentViewController:alertController animated:YES completion:nil];

有关 UIAlertController 私有 API 的更多信息,请参见 iPhone Dev Wiki 的 article on UIAlertController

我还会检查 _UIAlertControllerActionView and UIAlertAction, as well as the UIAlertActionViewRepresentation_Internal 协议上的私有接口。