在UIViewController上显示clearColor UIViewController

hig*_*ech 147 iphone objective-c storyboard uiviewcontroller ios

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

FirstViewController.m

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

[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:NO];  
Run Code Online (Sandbox Code Playgroud)

SecondViewController.m

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

决议:我解决了问题.它适用于iPhone和iPad.没有黑色背景的模态视图控制器只是clearColor/transparent.我需要改变的唯一一件事是我换成UIModalPresentationFullScreenUIModalPresentationCurrentContext.这有多简单啊!

FirstViewController.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];
Run Code Online (Sandbox Code Playgroud)

注意:如果您使用以下modalPresentationStyle财产navigationController:

FirstViewController.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];
Run Code Online (Sandbox Code Playgroud)

注意:坏消息是上述解决方案在iOS 7上无效.好消息是我修复了iOS7的问题!我问了一些人的帮助,这就是他说的话:

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

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

Bro*_*son 142

iOS8上+

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

MyModalViewController *modalViewController = [[MyModalViewController alloc] init];
modalViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;           
[self presentViewController:modalViewController animated:YES completion:nil];    
Run Code Online (Sandbox Code Playgroud)

  • 应该是接受的答案.之前接受的答案仍然有效,但是这是推荐的方法. (5认同)
  • 如果您只为ios 8开发,请使用此答案!有用 (3认同)
  • 为了为我工作,我添加了modalViewController.view.backgroundColor = UIColor.clearColor()感谢解决方案 (3认同)
  • 很好,但是最后一行不应该是[[self presentViewController:modalViewController animation:YEScomplete:nil];`?(而不是targetController)? (2认同)

hig*_*ech 138

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

FirstViewController.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];
Run Code Online (Sandbox Code Playgroud)

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

FirstViewController.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];
Run Code Online (Sandbox Code Playgroud)

注意:坏消息是上述解决方案在iOS 7上无效.好消息是我修复了iOS7的问题!我问了一些人的帮助,这就是他说的话:

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

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

  • 顺便说一句..我观察到因为self.modalPresentationStyle = UIModalPresentationCurrentContext; 它没有呈现modalViewController与动画..任何想法? (4认同)
  • @pkamb我更新了iOS 7的解决方案.http://stackoverflow.com/a/11252969/1344459我希望能帮到你. (3认同)

pas*_*vin 112

因此,对于纯视觉思想家和故事板粉丝,您可以:

1.呈现视图控制器

定义上下文

2.呈现View Controller

演讲:过度背景

  • 这个答案太甜了,我现在患有糖尿病. (16认同)

Kev*_*OUX 21

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)
Run Code Online (Sandbox Code Playgroud)


smi*_*Bot 16

这是来自使用控制拖动segue的xCode 7 beta 4.只需将目的地的背景设置为清除,然后在IB中设置segue属性(nb.演示文稿也可以是"全屏幕"):

在此输入图像描述

  • 谢谢.我做了你的segue修复+ @pasevin的ViewControllers答案,它工作了!! (2认同)

小智 8

我找到了让它在iOS7和iOS8上运行的最简单方法是在因为iOS8而在modallyPresentedVC(你想以模态方式呈现的ViewController)上添加将presentationStyle设置为UIModalPresentationOverCurrentContext:

[modallyPresentedVC setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[modallyPresentedVC.navigationController setModalPresentationStyle:UIModalPresentationOverCurrentContext];
Run Code Online (Sandbox Code Playgroud)

由于iOS7,在presentVC(呈现modallyPresented的控制器)上的UIModalPresentationCurrentContext和UIModalPresentationCurrentContext:

[presentingVC setModalPresentationStyle:UIModalPresentationCurrentContext];
[presentingVC.navigationController setModalPresentationStyle:UIModalPresentationCurrentContext];
Run Code Online (Sandbox Code Playgroud)

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


Moj*_*bye 5

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)
Run Code Online (Sandbox Code Playgroud)