首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >iOS:透明背景的模式ViewController

iOS:透明背景的模式ViewController
EN

Stack Overflow用户
提问于 2012-10-05 15:04:50
回答 16查看 171.6K关注 0票数 188

我试图以模态的方式呈现一个具有透明背景的视图控制器。我的目标是让呈现和呈现视图控制器的视图同时显示。问题是,当演示动画结束时,演示视图控制器的视图将消失。

代码语言:javascript
复制
- (IBAction)pushModalViewControllerButtonPressed:(id)sender
{
    ModalViewController *modalVC = [[ModalViewController alloc] init];
    [self presentViewController:modalVC animated:YES completion:nil];
}

我知道我可以将视图添加为一个子视图,但出于某些原因,我想避免这种解决方案。我怎么才能修复它呢?

EN

回答 16

Stack Overflow用户

发布于 2013-09-23 07:16:56

这段代码在iOS6和iOS7下的iPhone上运行良好:

代码语言:javascript
复制
presentedVC.view.backgroundColor = YOUR_COLOR; // can be with 'alpha'
presentingVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentingVC presentViewController:presentedVC animated:YES completion:NULL];

在这种情况下,您会错过幻灯片动画。要保留动画,您仍然可以使用以下“非优雅”扩展:

代码语言:javascript
复制
[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协议实现自定义过渡动画。当然,在这种情况下,你可以得到透明的背景

代码语言:javascript
复制
@interface ModalViewController : UIViewController <UIViewControllerTransitioningDelegate>

首先,在演示之前,您必须设置modalPresentationStyle

代码语言:javascript
复制
modalViewController.modalPresentationStyle = UIModalPresentationCustom;

然后,您必须实现两个协议方法

代码语言:javascript
复制
- (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类中定义自定义转换

代码语言:javascript
复制
@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
    }
}
票数 44
EN

Stack Overflow用户

发布于 2015-10-14 18:26:24

我与XCode 7的界面生成器进行了一点斗争,以设置@VenuGopalTewari建议的演示样式。在这个版本中,似乎没有用于段的Over Current ContextOver Full Screen演示模式。因此,为了使其正常工作,我将模式设置为Default

使用

此外,我将以模态方式呈现的视图控制器的呈现模式设置为Over Full Screen

票数 21
EN

Stack Overflow用户

发布于 2015-01-22 02:38:32

创建一个分段来以模态方式呈现,并将该分段的呈现属性设置为覆盖当前上下文,它将100%工作

票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12741224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档