How to use UIBlurEffect with modal View Controllers?

后端 未结 6 2389
夕颜
夕颜 2021-02-13 14:17

I have a UIViewController that presents another UIViewController modally. I want the modal view controller to have the blur/transparency that iOS 7 int

6条回答
  •  没有蜡笔的小新
    2021-02-13 15:00

    When using UIVisualEffectView, you don't need to generate a snapshot. Try removing "- (UIImage *)viewImage" and in the model view controller add a UIVisualEffectView with size matching the view controller view, and add all "control" views into the UIVisualEffectView's 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

    I haven't tried it with storyboards, but here's a tested working snippet. Might be a good place to start. Translating to objc should be pretty straightfoward

    Here's the presenting view controller

    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: {})
    }
    }
    

    Here's the modal

    import UIKit
    
    class ModalViewController: UIViewController {
    
    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, 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)
    }
    }
    

提交回复
热议问题