示例代碼
UIViewController *modalViewController = [[UIViewController alloc] init];
modalViewController.view.backgroundColor = [UIColor whiteColor];
if (@available(iOS 16.0, *)) {
modalViewController.modalPresentationStyle = UIModalPresentationPageSheet;
// iOS 15 提供 SheetPresentation
UISheetPresentationController *sheet = modalViewController.sheetPresentationController;
// iOS 16 支持為 SheetPresentation 自定義高度
UISheetPresentationControllerDetent *customDetent = [UISheetPresentationControllerDetent customDetentWithIdentifier:@"customDetent" resolver:^CGFloat(id<UISheetPresentationControllerDetentResolutionContext> context) {
// 根據上下文的信息來調整高度
return SGScreen_Height - 200;
}];
if (sheet) {
sheet.detents = @[
[UISheetPresentationControllerDetent mediumDetent],
[UISheetPresentationControllerDetent largeDetent],
customDetent
];
sheet.prefersGrabberVisible = YES;
sheet.selectedDetentIdentifier = @"customDetent";
sheet.prefersScrollingExpandsWhenScrolledToEdge = NO;
}
} else {
// Fallback on earlier versions
modalViewController.modalPresentationStyle = UIModalPresentationAutomatic;
}
[self presentViewController:modalViewController animated:YES completion:nil];
UISheetPresentationController
是 iOS 15 及以上版本中引入的一種用于呈現底部彈出視圖(Sheet)的控制器罗丰,提供了一種標準化的方式來管理模態(tài)視圖的展示衙荐。在 iOS 16 及以上版本中,UISheetPresentationController
更加強大,允許開發(fā)者通過 customDetentWithIdentifier:resolver:
方法自定義彈出視圖的高度耀鸦。