适配iPad的oc代码如下:\n\nobjective-c\nUIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleActionSheet];\n\n// Add actions\nUIAlertAction *action1 = [UIAlertAction actionWithTitle:@"Action 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {\n // Action 1 handler\n}];\n[alertController addAction:action1];\n\nUIAlertAction *action2 = [UIAlertAction actionWithTitle:@"Action 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {\n // Action 2 handler\n}];\n[alertController addAction:action2];\n\nUIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];\n[alertController addAction:cancelAction];\n\n// Configure popover presentation controller for iPad\nif (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {\n alertController.popoverPresentationController.sourceView = self.view;\n alertController.popoverPresentationController.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds), 0, 0);\n alertController.popoverPresentationController.permittedArrowDirections = 0;\n}\n\n// Present the action sheet\n[self presentViewController:alertController animated:YES completion:nil];\n\n\n这段代码创建了一个UIAlertControllerStyleActionSheet样式的UIAlertController,并添加了两个Action以及一个Cancel Action。在iPad上,通过设置popoverPresentationController的属性来调整弹出框的位置和显示方式。最后通过presentViewController:animated:completion:方法来显示弹出框。

iOS UIAlertControllerStyleActionSheet 适配iPad - Objective-C 代码示例

原文地址: https://www.cveoy.top/t/topic/qbMw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录