我试图以模态的方式呈现一个具有透明背景的视图控制器。我的目标是让呈现和呈现视图控制器的视图同时显示。问题是,当演示动画结束时,演示视图控制器的视图将消失。
- (IBAction)pushModalViewControllerButtonPressed:(id)sender
{
ModalViewController *modalVC = [[ModalViewController alloc] init];
[self presentViewController:modalVC animated:YES completion:nil];
}
我知道我可以将视图添加为一个子视图,但出于某些原因,我想避免这种解决方案。我怎么才能修复它呢?
发布于 2013-09-23 07:16:56
这段代码在iOS6和iOS7下的iPhone上运行良好:
presentedVC.view.backgroundColor = YOUR_COLOR; // can be with 'alpha'
presentingVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentingVC presentViewController:presentedVC animated:YES completion:NULL];
在这种情况下,您会错过幻灯片动画。要保留动画,您仍然可以使用以下“非优雅”扩展:
[presentingVC presentViewController:presentedVC animated:YES completion:^{
[presentedVC dismissViewControllerAnimated:NO completion:^{
presentingVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentingVC presentViewController:presentedVC animated:NO completion:NULL];
}];
}];
如果我们的presentingV位于UINavigationController或UITabbarController内部,您需要以presentingVC的身份使用该控制器进行操作。
此外,在iOS7中,您可以应用UIViewControllerTransitioningDelegate
协议实现自定义过渡动画。当然,在这种情况下,你可以得到透明的背景
@interface ModalViewController : UIViewController <UIViewControllerTransitioningDelegate>
首先,在演示之前,您必须设置modalPresentationStyle
modalViewController.modalPresentationStyle = UIModalPresentationCustom;
然后,您必须实现两个协议方法
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
CustomAnimatedTransitioning *transitioning = [CustomAnimatedTransitioning new];
transitioning.presenting = YES;
return transitioning;
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
CustomAnimatedTransitioning * transitioning = [CustomAnimatedTransitioning new];
transitioning.presenting = NO;
return transitioning;
}
最后一件事是在CustomAnimatedTransitioning
类中定义自定义转换
@interface CustomAnimatedTransitioning : NSObject <UIViewControllerAnimatedTransitioning>
@property (nonatomic) BOOL presenting;
@end
@implementation CurrentContextTransitionAnimator
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.25;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
if (self.presenting) {
// custom presenting animation
}
else {
// custom dismissing animation
}
}
发布于 2015-10-14 18:26:24
我与XCode 7的界面生成器进行了一点斗争,以设置@VenuGopalTewari建议的演示样式。在这个版本中,似乎没有用于段的Over Current Context
或Over Full Screen
演示模式。因此,为了使其正常工作,我将模式设置为Default
使用
此外,我将以模态方式呈现的视图控制器的呈现模式设置为Over Full Screen
发布于 2015-01-22 02:38:32
创建一个分段来以模态方式呈现,并将该分段的呈现属性设置为覆盖当前上下文,它将100%工作
https://stackoverflow.com/questions/12741224
复制相似问题