如何在iOS中呈现半透明(半切)viewcontroller?

如何在iOS中呈现半透明(半切)viewcontroller?,ios,uiviewcontroller,Ios,Uiviewcontroller,我使用以下代码来演示viewcontroller。 我的问题是:动画完成后,透明的主背景变成不透明的黑色 我如何修复它并使其保持清晰的颜色 UIViewController *menuViewController=[[UIViewController alloc]init]; menuViewController.view.backgroundColor=[UIColor clearColor]; menuViewController.view.tintColor=[UIColor

我使用以下代码来演示viewcontroller。 我的问题是:动画完成后,透明的主背景变成不透明的黑色

我如何修复它并使其保持清晰的颜色

UIViewController *menuViewController=[[UIViewController alloc]init];
   menuViewController.view.backgroundColor=[UIColor clearColor];
   menuViewController.view.tintColor=[UIColor clearColor];
   menuViewController.view.opaque=NO;

UIView *menuView=[[UIView alloc]initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height-200,320,200)];
   menuView.backgroundColor=[UIColor redColor];

[menuViewController.view addSubview:menuView];

[self presentViewController:menuViewController animated:YES completion:nil];

更新:我正在尝试查看“self”(演示者视图控制器的视图)的内容。

尝试使俯视图透明,并在所需视图下方添加另一个视图,使该视图的背景色为黑色,并将alpha设置为0.5或任何您喜欢的不透明度级别

更新

在大多数情况下,您需要遵循下面的指南。正如他提到的,
menuViewController.modalPresentationStyle=.overCurrentContext
是保持显示视图控制器可见的最简单的现代方法

我之所以保留这个答案,是因为它为OPs问题提供了最直接的解决方案,他们已经有了一个由当前视图控制器管理的视图,并且正在寻找一种方式来呈现它,因为它解释了问题的实际原因


正如在评论中提到的,这不是一个透明度问题(否则你会期望背景变成白色)。当
presentViewController:animated:completion:
动画完成时,显示视图控制器实际上从可视堆栈中移除。您看到的黑色是窗口的背景色


由于您刚刚使用<代码> MeNView控制器<代码>作为<代码> MunuView < /C> >的主机,以简化动画,您可以考虑跳过<>代码> MunuView控制器< /C> >,将<>代码> MunuView < /C> >添加到您现有的视图控制器视图层次结构中,并自己制作动画。

从iOS 3.2开始,您应该使用属性modalPresentationStyle

例如:

presenterViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[presenterViewController presentViewController:loginViewController animated:YES completion:NULL];

在iOS 7中,您可以显示一个视图控制器,并且仍然可以在其下方看到原始视图控制器,就像表单一样。为此,您需要做两件事:

  • 将模态表示样式设置为“自定义”:

    viewControllerToPresent.modalPresentationStyle = UIModalPresentationCustom;
    
  • 设置转换委托:

    viewControllerToPresent.transitioningDelegate = self;
    
  • 在本例中,我们将委托设置为self,但它可以是另一个对象。学员需要实现协议所需的两种方法,可能类似于:

    - (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
    {
        SemiModalAnimatedTransition *semiModalAnimatedTransition = [[SemiModalAnimatedTransition alloc] init];
        semiModalAnimatedTransition.presenting = YES;
        return semiModalAnimatedTransition;
    }
    
    - (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
    {
        SemiModalAnimatedTransition *semiModalAnimatedTransition = [[SemiModalAnimatedTransition alloc] init];
        return semiModalAnimatedTransition;
    }
    
    以及实施:

    #import "SemiModalAnimatedTransition.h"
    
    @implementation SemiModalAnimatedTransition
    
    - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
    {
        return self.presenting ? 0.6 : 0.3;
    }
    
    - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
    {
        UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
        UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
        CGRect endFrame = fromViewController.view.bounds;
    
        if (self.presenting) {
            fromViewController.view.userInteractionEnabled = NO;
    
            [transitionContext.containerView addSubview:fromViewController.view];
            [transitionContext.containerView addSubview:toViewController.view];
    
            CGRect startFrame = endFrame;
            startFrame.origin.y = endFrame.size.height;
    
            toViewController.view.frame = startFrame;
    
            [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
                fromViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
                toViewController.view.frame = endFrame;
            } completion:^(BOOL finished) {
                [transitionContext completeTransition:YES];
            }];
        }
        else {
            toViewController.view.userInteractionEnabled = YES;
    
            [transitionContext.containerView addSubview:toViewController.view];
            [transitionContext.containerView addSubview:fromViewController.view];
    
            endFrame.origin.y = endFrame.size.height;
    
            [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
                toViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic;
                fromViewController.view.frame = endFrame;
            } completion:^(BOOL finished) {
                [transitionContext completeTransition:YES];
            }];
        }
    }
    
    @end
    

    这是一个相当简单的问题。您只需要为正在显示的视图控制器设置modalPresentationStyle,而不是创建自定义视图转换。 此外,还应在序列图像板/通过代码中为显示的视图控制器设置背景色(和alpha值)

    CustomViewController: UIViewController {
        override func viewDidLoad() {
          super.viewDidLoad()
          view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
        }
    }
    
    在显示视图控制器的iAction组件中-

    let vc = storyboard?.instantiateViewControllerWithIdentifier("customViewController") as! CustomViewController
    vc.modalPresentationStyle = UIModalPresentationStyle.Custom
    presentViewController(vc, animated: true, completion: nil)
    

    这不是透明度问题。动画完成后,iOS将从屏幕上删除隐藏的视图控制器。您看到的黑色是窗口的背景色。我相信iOS7对此有一些选项。@BrianNickel,你的意思是“演示者视图”控制器将隐藏,直到“演示者视图”控制器被取消?在这种情况下,我是否应该手动添加视图并使用动画将其从屏幕底部显示出来?这是正确的。您可以跳过
    menuViewController
    ,然后在屏幕上随意设置
    menuView
    的动画。请发布一个答案,其中提到您刚才给出的主要解释,好吗?(无法通过演示视图控制器查看演示者视图控制器)。所以我会把它标记为答案,其他用户可以很容易地找到原因。非常感谢。不,添加视图上的任何“交互”都由其原始视图控制器处理。但是,由于它的父视图在添加到另一个视图后发生了更改,链接链就断了。要正确传播方法调用(例如viewDidAppear),将子控制器添加为子控制器也很重要。这并不能解决问题。presenterViewController仍然从屏幕上移除。从儿童VCs或其他模态进行演示时,也有一些怪癖。可在
    PareForSegue
    中设置
    modalPresentationStyle
    TransitionLegate
    ,这应标记为最佳答案这在iOS 8中中断。关闭屏幕时,屏幕保持白色删除以下行修复了问题
    [transitionContext.containerView addSubview:toViewController.view];[transitionContext.containerView添加子视图:fromViewController.view]
    在iOS8+中,自定义转换在关闭时失败,因为它不希望在任何时候将
    presentingViewController
    的视图移动到
    transitionContext.containerView
    。删除该视图后,演示者将随之删除。删除这些行将修复8+的解决方案(可能仍然适用于7)。@RicSantos谢谢!是的,这只在iOS 8+上测试过。
    CustomViewController: UIViewController {
        override func viewDidLoad() {
          super.viewDidLoad()
          view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
        }
    }
    
    let vc = storyboard?.instantiateViewControllerWithIdentifier("customViewController") as! CustomViewController
    vc.modalPresentationStyle = UIModalPresentationStyle.Custom
    presentViewController(vc, animated: true, completion: nil)