ios – 如何使用UIBlurEffect与模态视图控制器?

我有一个UIViewController,以模态方式呈现另一个UIViewController.我想要模态视图控制器具有iOS 7引入的模糊/透明度.我尝试使用新的UIVisualEffect,但它似乎只适用于UIViews,而不是UIViewControllers?

这是我写的代码,我添加为子视图的所有视图都是我想要在用户界面上方的模糊视图下方的.

在演示视图控制器中,我将屏幕截图显示给模糊视图控制器,然后再应用模糊.

在呈现视图控制器:

- (UIImage *)viewImage {
    CGSize size = CGSizeMake(self.view.frame.size.width,self.view.frame.size.height);
    UIGraphicsBeginImageContext(size);
    [self.view drawViewHierarchyInRect:(CGRect){CGPointZero,self.view.frame.size.width,self.view.frame.size.height} afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetimageFromCurrentimageContext();
    UIGraphicsEndImageContext();

    return image;
}

在模态视图控制器:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:[[UIImageView alloc] initWithImage:self.backgroundImage]];
    //self.backgroundImage is the image that the method above returns,it's a screenshot of the presenting view controller.
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    [blurEffectView setFrame:self.view.bounds];
    [self.view addSubview:blurEffectView];

    [self.view bringSubviewToFront:self.cancelButton];
    [self.view bringSubviewToFront:self.titleLabel];
    [self.view bringSubviewToFront:self.tableView];
}

解决方法

使用UIVisualEffectView时,不需要生成快照.尝试删除“ – (UIImage *)viewImage”,并在模型视图控制器中添加一个与视图控制器视图大小匹配的UIVisualEffectView,并将所有“控制”视图添加到UIVisualEffectView的contentView中.
- (void)viewDidLoad {
    [super viewDidLoad];

    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    [blurEffectView setFrame:self.view.bounds];
    [self.view addSubview:blurEffectView];

    [blurEffectView.contentView addSubview:self.cancelButton];
    [blurEffectView.contentView addSubview:self.titleLabel];
    [blurEffectView.contentView addSubview:self.tableView];
}

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIVisualEffectView/index.html

我没有尝试过故事板,但这是一个经过测试的工作片段.可能是一个开始的好地方.翻译成objc应该是挺直的

这是演示视图控制器

import UIKit

class BaseViewController: UIViewController {

init(nibName nibNameOrNil: String?,bundle nibBundleOrNil: NSBundle?) {
    super.init(nibName: nibNameOrNil,bundle: nibBundleOrNil)
}

override func viewDidLoad() {
    super.viewDidLoad()

    var backgroundImage = UIImageView(image: UIImage(named: "image"))
    self.view.addSubview(backgroundImage)

    var button = UIButton(frame: CGRectMake(0,100,320,50))
    button.setTitle("Lorem Ipsum",forState: UIControlState.normal)
    button.backgroundColor = UIColor.redColor()
    button.addTarget(self,action: "onButtonTapped:",forControlEvents: UIControlEvents.TouchUpInside)

    self.view.addSubview(button)
}

func onButtonTapped(sender: UIButton?) {
    var modal = ModalViewController(nibName: nil,bundle: nil)
    modal.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
    self.presentViewController(modal,animated: true,completion: {})
}
}

这是模态

import UIKit

class ModalViewController: UIViewController {

init(nibName nibNameOrNil: String?,bundle: nibBundleOrNil)
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.clearColor()

    let effect = UIBlurEffect(style: UIBlurEffectStyle.Light)
    let blurView = UIVisualEffectView(effect: effect)

    blurView.frame = self.view.bounds

    self.view.addSubview(blurView)
}
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...