SYPopoverController
是 UIPresentationController
的子类,用于在屏幕中央展示 UIViewController
,并具有所需的大小 preferredContentSize
。
PS:抱歉,这不是最好的名字,但 SYPopupController
已经被占用了。
示例代码
// 1. The controllers your want to present in the popover
UINavigationController *navController = [[UINavigationController alloc] init];
UIViewController *viewController = [[UIViewController alloc] init];
[navController setViewControllers:@[viewController]];
// 2. Prepare the navigationController to use SYPopoverController
[navController setModalPresentationStyle:UIModalPresentationCustom];
[navController setTransitioningDelegate:[SYPopoverTransitioningDelegate shared]];
// 3. Present
[self presentViewController:navController animated:YES completion:nil];
// (Optional) 4. Access the popoverController to use the background you wish
// Here we use a semi-transparent white color
SYPopoverController *popoverController = (SYPopoverController *)navController.presentationController;
[popoverController.backgroundView setBackgroundColor:[UIColor colorWithWhite:1. alpha:0.6]];
// Alternative to 2+3: use the helper method
[self sy_presentPopover:navController animated:YES completion:nil];
自定义
背景
默认情况下没有背景。您有两种方法可以更改这种行为:
-
在展示视图控制器中从
SYPopoverContentViewDelegate
实现以下方法popoverControllerBackgroundColor:
。 -
使用以下代码访问背景视图,并更改其背景颜色或添加子视图:
[(SYPopoverController *)myViewController.presentationController backgroundView]
取消
默认情况下,当用户触摸 popoverController
的背景时,它将取消。您可以通过遵循 SYPopoverContentViewDelegate
协议并实现 popoverControllerShouldDismissOnBackgroundTap:
方法来覆盖此行为。
许可证
您可以根据需要在任何项目中使用它,重新分发时提及我的名字,如果它出现问题请不要责怪我 :)
-- dvkch