iOS UIAlertControllerStyleActionSheet iPad 适配指南
在适配 iPad 上的 UIAlertControllerStyleActionSheet,可以通过以下方式进行处理:\n\n1. 检查设备类型:可以通过 UIDevice 类的 userInterfaceIdiom 属性检查设备类型,如果是 iPad,则使用 UIAlertControllerStyleActionSheet,否则使用其他方式显示选项。\n\nswift\nif UIDevice.current.userInterfaceIdiom == .pad {\n // 在 iPad 上使用 UIAlertControllerStyleActionSheet\n let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)\n // 添加选项和处理事件\n alertController.addAction(UIAlertAction(title: "Option 1", style: .default, handler: { action in\n // 处理选项1的事件\n }))\n alertController.addAction(UIAlertAction(title: "Option 2", style: .default, handler: { action in\n // 处理选项2的事件\n }))\n alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))\n // 在 iPad 上使用弹出视图控制器显示选项\n alertController.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem\n present(alertController, animated: true, completion: nil)\n} else {\n // 在 iPhone 上使用其他方式显示选项\n // ...\n}\n\n\n2. 自定义弹出位置:使用 UIAlertController 的 popoverPresentationController 属性来指定弹出位置。可以将其设置为某个视图的位置,例如 UIBarButtonItem 或者一个 CGRect。\n\nswift\nalertController.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem\n// 或者\nalertController.popoverPresentationController?.sourceView = someView\nalertController.popoverPresentationController?.sourceRect = someRect\n\n\n通过上述方式,在 iPad 上适配 UIAlertControllerStyleActionSheet,可以根据设备类型和弹出位置进行相应的处理。
原文地址: https://www.cveoy.top/t/topic/qbMt 著作权归作者所有。请勿转载和采集!