iOS 7半透明模态视图控制器

iOS 7上的应用程序商店应用程序使用磨砂玻璃效果,可以查看后面的视图。 这是使用iOS 7内置的API还是自定义代码。 我希望这将是前者,但我不能在文档中看到任何明显的引用。 显而易见的事情(像设置模态视图上的alpha属性)似乎没有任何影响。

要查看示例,请打开App Store应用程序,然后按右上angular的button。

App Store主页在App Store中的模态视图

随着iOS 8.0的发布,不再需要获取图像和模糊了。 正如Andrew Plummer指出的那样,您可以使用UIVisualEffectView和UIBlurEffect 。

UIViewController * contributeViewController = [[UIViewController alloc] init]; UIBlurEffect * blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; UIVisualEffectView *beView = [[UIVisualEffectView alloc] initWithEffect:blurEffect]; beView.frame = self.view.bounds; contributeViewController.view.frame = self.view.bounds; contributeViewController.view.backgroundColor = [UIColor clearColor]; [contributeViewController.view insertSubview:beView atIndex:0]; contributeViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext; [self presentViewController:contributeViewController animated:YES completion:nil]; 

iOS 8之前的解决scheme

我想就rckoenes的回答作出延伸:

正如所强调的,您可以通过以下方式创build这种效果

  1. 将底层的UIView转换为UIImage
  2. 模糊UIImage
  3. 将UIImage设置为您的视图的背景。

在这里输入图像说明


听起来像很多工作,但实际上很简单:

1.创build一个UIView的类别,并添加以下方法:

 -(UIImage *)convertViewToImage { UIGraphicsBeginImageContext(self.bounds.size); [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 

2.使用Apple的图像效果类别 ( 下载 )制作当前视图的图像并将其模糊,

 UIImage* imageOfUnderlyingView = [self.view convertViewToImage]; imageOfUnderlyingView = [imageOfUnderlyingView applyBlurWithRadius:20 tintColor:[UIColor colorWithWhite:1.0 alpha:0.2] saturationDeltaFactor:1.3 maskImage:nil]; 

3.将其设置为叠加层的背景。

 -(void)viewDidLoad { self.view.backgroundColor = [UIColor clearColor]; UIImageView* backView = [[UIImageView alloc] initWithFrame:self.view.frame]; backView.image = imageOfUnderlyingView; backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6]; [self.view addSubview:backView]; } 

刚刚在Swift中重新实现了Sebastian Hojas的解决scheme:

1.创build一个UIView扩展并添加以下方法:

 extension UIView { func convertViewToImage() -> UIImage{ UIGraphicsBeginImageContext(self.bounds.size); self.drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext(); return image; } } 

2.制作当前视图的图像,并使用Apple的图像效果进行模糊处理(我发现在Swift中重新实现了这个效果: SwiftUIImageEffects

 var imageOfUnderlyingView = self.view.convertViewToImage() imageOfUnderlyingView = imageOfUnderlyingView.applyBlurWithRadius(2, tintColor: UIColor(white: 0.0, alpha: 0.5), saturationDeltaFactor: 1.0, maskImage: nil)! 

3.将其设置为叠加层的背景。

 let backView = UIImageView(frame: self.view.frame) backView.image = imageOfUnderlyingView backView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5) view.addSubview(backView) 

我认为这是一个模式视图控制器的最简单的解决scheme,覆盖一切都有一个很好的模糊(iOS8)

  UIViewController * contributeViewController = [[UIViewController alloc] init]; UIBlurEffect * blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; UIVisualEffectView *beView = [[UIVisualEffectView alloc] initWithEffect:blurEffect]; beView.frame = self.view.bounds; contributeViewController.view.frame = self.view.bounds; contributeViewController.view.backgroundColor = [UIColor clearColor]; [contributeViewController.view insertSubview:beView atIndex:0]; contributeViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext; [self presentViewController:contributeViewController animated:YES completion:nil]; 

iOS 7 SDK中没有可用的API,可以让你“视”下视图控制器。

我所做的就是把一种形象渲染成一种形象,然后把这种形象作为背景来呈现。

苹果提供了一个很好的例子: https : //developer.apple.com/downloads/index.action?name=WWDC%202013

你想要的项目叫做iOS_RunningWithASnap

一个简单的方法来实现这个(基于安德鲁Plummer的答案)与界面生成器(也消除副作用,出现在安德鲁斯答案):

  • 在IB中,在您​​的其他视图下将视觉效果视图添加到您的视图控制器;
  • 从视觉效果视图顶部(父)视图,使所有的顶部,底部,左,右约束设置为0;
  • 设置模糊风格;
  • 添加代码,您展示您的新花式视图控制器:

 UIViewController *fancyViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"yourStoryboardIDFOrViewController"]; mainButtonViewController.view.backgroundColor = [UIColor clearColor]; fancyViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext; [self presentViewController:fancyViewController animated:YES completion:nil]; 

实际上,第二行和第三行非常重要,否则控制器会闪烁,然后变黑。

从iOS 8开始,这个工作:

  let vc = UIViewController() vc.view = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) vc.modalPresentationStyle = .OverFullScreen let nc = UINavigationController(rootViewController: vc) nc.modalPresentationStyle = .OverFullScreen presentViewController(nc, animated: true, completion: nil) 

关键是.OverFullScreen标志,并确保viewControllers有一个模糊UIVisualEffectView这是第一个可见的看法。

正如@ rckoenes所说,苹果没有提供框架来获得这种效果。 但是那里的一些人已经build立了很好的select,比如这个例子:

https://github.com/JagCesar/iOS-blur/

在iOS 5和iOS 6上也可以使用的几种替代方法:

FXBlurView: https : //github.com/nicklockwood/FXBlurView

iOS RealtimeBlur: https : //github.com/alexdrone/ios-realtimeblur

我已经把模糊的视图控制器上传到[GitHub] [1]。 它还带有一个segue子类,所以你可以在故事板中使用它。

存储库: https : //github.com/datinc/DATBlurSegue

快速和容易的解决scheme与XIB支持,您可以使用老学校的男孩https://github.com/cezarywojcik/CWPopup

您可以将viewController作为子视图控件添加,并创build一个自定义animation,而不是将其视为模态视图。 您只需要将viewController的默认视图更改为viewDidLoad中的UIToolBar即可。

这将允许您尽可能模仿appstore的模糊模式视图。

苹果发布了这些效果的UIImageEffect类别。 这些类别应该手动添加到项目中,并支持iOS7。

您可以使用UIToolbar作为背景。 默认情况下,UIToolbar具有50px的高度。 在UIToolbar上添加自动布局约束。 然后select高度约束并修改它。

层次结构如下所示:

 UIView -> clear colour for background. - UIToolbar - Other contents.