ios 在 UIViewController 上显示 clearColor UIViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11236367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:59:57  来源:igfitidea点击:

Display clearColor UIViewController over UIViewController

iphoneobjective-ciosuiviewcontrollerstoryboard

提问by hightech

I have a UIViewControllerview as a subview/modal on top of another UIViewControllerview, such as that the subview/modal should be transparent and whatever components is added to the subview should be visible. The problem is that I have is the subview shows black background instead to have clearColor. I'm trying to make UIViewas a clearColor not black background. Does anybody know what is wrong with it? Any suggestion appreciated.

我有一个UIViewController视图作为另一个UIViewController视图之上的子视图/模式,例如子视图/模式应该是透明的,并且添加到子视图的任何组件都应该是可见的。问题是我的子视图显示黑色背景而不是 clearColor。我试图做UIView一个clearColor不是黑色的背景。有人知道它有什么问题吗?任何建议表示赞赏。

FirstViewController.m

第一视图控制器.m

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];

[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:NO];  

SecondViewController.m

第二视图控制器.m

- (void)viewDidLoad 
{
     [super viewDidLoad];
     self.view.opaque = YES;
     self.view.backgroundColor = [UIColor clearColor];
}


RESOLVED: I fixed the issues. It is working so well for both of iPhone and iPad. Modal View Controller with no black background just clearColor/transparent. The only thing that I need to change is I replaced UIModalPresentationFullScreento UIModalPresentationCurrentContext. How simple is that!

已解决:我修复的问题。它适用于 iPhone 和 iPad。没有黑色背景的模态视图控制器只是 clearColor/transparent。我需要改变的唯一的事情是我更换UIModalPresentationFullScreenUIModalPresentationCurrentContext。那是多么简单!

FirstViewController.m

第一视图控制器.m

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];


NOTICE:If you are using a modalPresentationStyleproperty of navigationController:

注意:如果您使用的是以下modalPresentationStyle属性navigationController

FirstViewController.m

第一视图控制器.m

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];


NOTICE: The bad news is that the above solution doesn't work on iOS 7. The good news is that I fixed the issue for iOS7! I asked somebody for help and here is what he said:

注意:坏消息是上述解决方案不适用于 iOS 7。好消息是我修复了 iOS7 的问题!我向某人寻求帮助,这是他说的:

When presenting a view controller modally, iOS removes the view controllers underneath it from the view hierarchy for the duration it is presented. While the view of your modally presented view controller is transparent, there is nothing underneath it except the app window, which is black. iOS 7 introduced a new modal presentation style, UIModalPresentationCustom, that causes iOS not to remove the views underneath the presented view controller. However, in order to use this modal presentation style, you must provide your own transition delegate to handle the presentation and dismiss animations. This is outlined in the 'Custom Transitions Using View Controllers' talk from WWDC 2013 https://developer.apple.com/wwdc/videos/?id=218which also covers how to implement your own transition delegate.

当以模态方式呈现视图控制器时,iOS 会在它呈现的持续时间内从视图层次结构中删除它下面的视图控制器。虽然以模态呈现的视图控制器的视图是透明的,但它下面除了应用程序窗口之外没有任何东西,它是黑色的。iOS 7 引入了一种新的模态呈现样式,UIModalPresentationCustom,这会导致 iOS 不会删除呈现的视图控制器下的视图。但是,为了使用这种模态呈现样式,您必须提供自己的过渡委托来处理呈现和关闭动画。这在 WWDC 2013 https://developer.apple.com/wwdc/videos/?id=218的“使用视图控制器的自定义转换”演讲中有所概述,其中还介绍了如何实现您自己的转换委托。

You may see my solution for the above issue in iOS7: https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions

您可以在 iOS7 中看到我针对上述问题的解决方案:https: //github.com/hightech/iOS-7-Custom-ModalViewController-Transitions

采纳答案by hightech

RESOLVED: I fixed the issues. It is working so well for both of iPhone and iPad. Modal View Controller with no black background just clearColor/transparent. The only thing that I need to change is I replaced UIModalPresentationFullScreen to UIModalPresentationCurrentContext. How simple is that!

已解决:我修复的问题。它适用于 iPhone 和 iPad。没有黑色背景的模态视图控制器只是 clearColor/transparent。我唯一需要改变的是我将 UIModalPresentationFullScreen 替换为 UIModalPresentationCurrentContext。那是多么简单!

FirstViewController.m

第一视图控制器.m

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    vc.view.backgroundColor = [UIColor clearColor];
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentViewController:vc animated:NO completion:nil];


NOTICE:If you are using a modalPresentationStyle property of navigationController:

注意:如果您使用的是 navigationController 的 modalPresentationStyle 属性:

FirstViewController.m

第一视图控制器.m

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    vc.view.backgroundColor = [UIColor clearColor];
    self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentViewController:vc animated:NO completion:nil];


NOTICE: The bad news is that the above solution doesn't work on iOS 7. The good news is that I fixed the issue for iOS7! I asked somebody for help and here is what he said:

注意:坏消息是上述解决方案不适用于 iOS 7。好消息是我修复了 iOS7 的问题!我向某人寻求帮助,这是他说的:

When presenting a view controller modally, iOS removes the view controllers underneath it from the view hierarchy for the duration it is presented. While the view of your modally presented view controller is transparent, there is nothing underneath it except the app window, which is black. iOS 7 introduced a new modal presentation style, UIModalPresentationCustom, that causes iOS not to remove the views underneath the presented view controller. However, in order to use this modal presentation style, you must provide your own transition delegate to handle the presentation and dismiss animations. This is outlined in the 'Custom Transitions Using View Controllers' talk from WWDC 2013 https://developer.apple.com/wwdc/videos/?id=218which also covers how to implement your own transition delegate.

当以模态方式呈现视图控制器时,iOS 会在它呈现的持续时间内从视图层次结构中删除它下面的视图控制器。虽然以模态呈现的视图控制器的视图是透明的,但它下面除了应用程序窗口之外没有任何东西,它是黑色的。iOS 7 引入了一种新的模式呈现样式 UIModalPresentationCustom,它导致 iOS 不会删除呈现的视图控制器下的视图。但是,为了使用这种模态呈现样式,您必须提供自己的过渡委托来处理呈现和关闭动画。这在 WWDC 2013 https://developer.apple.com/wwdc/videos/?id=218的“使用视图控制器的自定义转换”演讲中有所概述,其中还介绍了如何实现您自己的转换委托。

You may see my solution for the above issue in iOS7: https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions

您可以在 iOS7 中看到我针对上述问题的解决方案:https: //github.com/hightech/iOS-7-Custom-ModalViewController-Transitions

回答by Brody Robertson

iOS8+

iOS8+

In iOS8+ you can now use the new modalPresentationStyle UIModalPresentationOverCurrentContext to present a view controller with a transparent background:

在 iOS8+ 中,您现在可以使用新的 modalPresentationStyle UIModalPresentationOverCurrentContext 来呈现具有透明背景的视图控制器:

MyModalViewController *modalViewController = [[MyModalViewController alloc] init];
modalViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;           
[self presentViewController:modalViewController animated:YES completion:nil];    

回答by pasevin

So for purely visual thinkers and storyboard fans, you can do:

因此,对于纯粹的视觉思想家和故事板迷,您可以:

1. Presenting View Controller

1. 呈现视图控制器

Define context

定义上下文

2. Presented View Controller

2. 呈现的视图控制器

Presentation: Over Current Context

演示文稿:当前上下文

回答by Kevin ABRIOUX

Swift 3 & iOS10 solution :

Swift 3 & iOS10 解决方案:

//create view controller
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "RoadTripPreviewViewController")
//remove black screen in background
vc.modalPresentationStyle = .overCurrentContext
//add clear color background
vc.view.backgroundColor = UIColor.clear
//present modal
self.present(vc, animated: true, completion: nil)

回答by smileBot

This is from xCode 7 beta 4 using a control drag segue. Simply set the background of your destination to clear, and set the segue properties in IB as this (nb. Presentation can also be "Over Full Screen"):

这是来自 xCode 7 beta 4 使用控件拖动 segue。只需将目的地的背景设置为清除,并将 IB 中的 segue 属性设置为这样(注意,演示也可以是“全屏显示”):

enter image description here

在此处输入图片说明

回答by 3vangelos

I found the easiest way to get it to work on iOS7 and iOS8 is to add set the presentationStyle to UIModalPresentationOverCurrentContext on the modallyPresentedVC (ViewController which you want to present modally) because of iOS8:

我发现让它在 iOS7 和 iOS8 上工作的最简单的方法是在 modallyPresentedVC(你想以模态呈现的视图控制器)上添加presentationStyle 到 UIModalPresentationOverCurrentContext 因为iOS8:

[modallyPresentedVC setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[modallyPresentedVC.navigationController setModalPresentationStyle:UIModalPresentationOverCurrentContext];

and UIModalPresentationCurrentContext on presentingVC (the controller which presents the modallyPresented) because of iOS7:

和 UIModalPresentationCurrentContext 在presentingVC(呈现模态呈现的控制器)上,因为iOS7:

[presentingVC setModalPresentationStyle:UIModalPresentationCurrentContext];
[presentingVC.navigationController setModalPresentationStyle:UIModalPresentationCurrentContext];

Because things are handled differently on iOS7 and iOS8. Of course you don't have to set the navigationController properties if you are not using one. Hope that helps.

因为在 iOS7 和 iOS8 上的处理方式不同。当然,如果您不使用的话,您不必设置 navigationController 属性。希望有帮助。

回答by Mojtabye

Swift2 version :

Swift2 版本:

let vc = self.storyboard!.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController
vc.view.backgroundColor = UIColor.clearColor()
vc.modalPresentationStyle = UIModalPresentationStyle.OverFullScreen // orOverCurrentContext to place under navigation
self.presentViewController(vc, animated: true, completion: nil)

回答by educaPix

Another way (no need to create custom transition and works on iOS 7)

另一种方式(无需创建自定义过渡并适用于 iOS 7)

Using Storyboard:

使用故事板:

Create the Child View Controller with freedom size, set view width to 500x500 (for example) and add the next method:

创建具有自由大小的子视图控制器,将视图宽度设置为 500x500(例如)并添加下一个方法:

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.view.superview.bounds = CGRectMake(0, 0, 500, 500);
    self.view.superview.backgroundColor = [UIColor clearColor];
}

Then create a Modal segue with Form Sheet and test it.

然后用 Form Sheet 创建一个 Modal segue 并测试它。

回答by Max Gribov

iOS 7 solution with custom segue:

带有自定义转场的 iOS 7 解决方案:

CustomSegue.h
#import <UIKit/UIKit.h>

    @interface CustomSegue : UIStoryboardSegue <UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning>

    @end



CustomSegue.m
#import "CustomSegue.h"

@implementation CustomSegue

-(void)perform {

    UIViewController* destViewController = (UIViewController*)[self destinationViewController];
    destViewController.view.backgroundColor = [UIColor clearColor];
    [destViewController setTransitioningDelegate:self];
    destViewController.modalPresentationStyle = UIModalPresentationCustom;
    [[self sourceViewController] presentViewController:[self destinationViewController] animated:YES completion:nil];
}


//===================================================================
// - UIViewControllerAnimatedTransitioning
//===================================================================

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
    return 0.25f;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {

    UIView *inView = [transitionContext containerView];
    UIViewController* toVC = (UIViewController*)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController* fromVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    [inView addSubview:toVC.view];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    [toVC.view setFrame:CGRectMake(0, screenRect.size.height, fromVC.view.frame.size.width, fromVC.view.frame.size.height)];

    [UIView animateWithDuration:0.25f
                     animations:^{

                         [toVC.view setFrame:CGRectMake(0, 0, fromVC.view.frame.size.width, fromVC.view.frame.size.height)];
                     }
                     completion:^(BOOL finished) {
                         [transitionContext completeTransition:YES];
                     }];
}


//===================================================================
// - UIViewControllerTransitioningDelegate
//===================================================================

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {

    return self;
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    //I will fix it later.
    //    AnimatedTransitioning *controller = [[AnimatedTransitioning alloc]init];
    //    controller.isPresenting = NO;
    //    return controller;
    return nil;
}

- (id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator {
    return nil;
}

- (id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator {
    return nil;
}

@end

Solution based on hightech code.

基于高科技代码的解决方案。

回答by Manab Kumar Mal

For Me this Works:

对我来说这有效:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"MMPushNotificationViewController"];

    vc.view.backgroundColor = [UIColor clearColor];
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
#ifdef __IPHONE_8_0
    if(IS_OS_8_OR_LATER)
    {
        self.providesPresentationContextTransitionStyle = YES;
        self.definesPresentationContext = YES;
        [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    }
#endif


    [self presentViewController:vc animated:NO completion:nil];

MMPushNotificationViewControlleris the Transparent View controller and also I have made the MMPushNotificationViewController's view color as clearcolor. Now All that I have Done and made my Transparentviewcontroller.

MMPushNotificationViewController是透明视图控制器,而且我已将MMPushNotificationViewController的视图颜色设为 clearcolor。现在我已经完成并制作了我的透明视图控制器。