
当然!UIModalPresentationStyle 是 iOS 开发中用于定义模态视图控制器(modal view controller)展示方式的一个枚举。以下是一些常见的 UIModalPresentationStyle 及其使用示例:
常见的 UIModalPresentationStyle
UIModalPresentationFullScreen (已废弃,推荐使用 UIModalPresentationCoverVertical)
- 整个屏幕被模态视图控制器覆盖。
UIModalPresentationPageSheet
- 模态视图控制器以页面表单的形式出现,通常具有圆角和阴影效果,并且尺寸小于全屏。
UIModalPresentationFormSheet
- 类似于 PageSheet,但更小一些,适合用作表单或对话框。
UIModalPresentationCurrentContext
- 模态视图控制器的呈现样式由当前上下文决定。如果父视图控制器本身是以模态形式呈现的,则子视图控制器也会继承相同的模态样式。
UIModalPresentationCustom
- 使用自定义的转场动画来展示模态视图控制器。
UIModalPresentationOverFullScreen
- 模态视图控制器会覆盖在全屏内容之上,但不会改变底层内容的布局。
UIModalPresentationPopover
- 通常与 iPad 上的 UIPopoverController 配合使用,呈现一个弹出窗口。
UIModalPresentationNone
- 不使用任何特殊的模态样式,等同于标准的 push 或 present 方式。
UIModalPresentationBlurOverFullScreen
- 在 iOS 8 中引入,模态视图控制器会以模糊背景的方式覆盖全屏内容。
UIModalPresentationCoverVertical
- 从底部向上滑动覆盖全屏内容,是 FullScreen 的现代替代方案。
示例代码
下面是一个简单的例子,展示了如何使用不同的 UIModalPresentationStyle 来呈现模态视图控制器:
#import "ViewController.h" #import "ModalViewController.h" // 假设你有一个 ModalViewController 类 @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 添加按钮来演示不同的模态展示样式 UIButton *fullScreenButton = [UIButton buttonWithType:UIButtonTypeSystem]; fullScreenButton.frame = CGRectMake(50, 100, 200, 50); [fullScreenButton setTitle:@"Full Screen (CoverVertical)" forState:UIControlStateNormal]; [fullScreenButton addTarget:self action:@selector(presentFullScreenModal) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:fullScreenButton]; UIButton *pageSheetButton = [UIButton buttonWithType:UIButtonTypeSystem]; pageSheetButton.frame = CGRectMake(50, 160, 200, 50); [pageSheetButton setTitle:@"Page Sheet" forState:UIControlStateNormal]; [pageSheetButton addTarget:self action:@selector(presentPageSheetModal) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:pageSheetButton]; UIButton *formSheetButton = [UIButton buttonWithType:UIButtonTypeSystem]; formSheetButton.frame = CGRectMake(50, 220, 200, 50); [formSheetButton setTitle:@"Form Sheet" forState:UIControlStateNormal]; [formSheetButton addTarget:self action:@selector(presentFormSheetModal) forControlEvents:UIControlEventTouchUpInside]; } - (void)presentFullScreenModal { ModalViewController *modalVC = [[ModalViewController alloc] init]; modalVC.modalPresentationStyle = UIModalPresentationCoverVertical; // 或者使用 UIModalPresentationFullScreen (已废弃) [self presentViewController:modalVC animated:YES completion:nil]; } - (void)presentPageSheetModal { ModalViewController *modalVC = [[ModalViewController alloc] init]; modalVC.modalPresentationStyle = UIModalPresentationPageSheet; [self presentViewController:modalVC animated:YES completion:nil]; } - (void)presentFormSheetModal { ModalViewController *modalVC = [[ModalViewController alloc] init]; modalVC.modalPresentationStyle = UIModalPresentationFormSheet; modalVC.preferredContentSize = CGSizeMake(300, 400); // 设置 FormSheet 的大小 [self presentViewController:modalVC animated:YES completion:nil]; } @end在这个例子中,我们创建了一个主视图控制器 (ViewController),并在其中添加了三个按钮,每个按钮都会以不同的 UIModalPresentationStyle 展示一个模态视图控制器 (ModalViewController)。你可以根据需要调整这些示例以适应你的应用程序。
