4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UIViewに対してBlur処理を入れる

Posted at

概要

  • UIViewにたいしてBlur処理をかけるときに使用します。
  • insertSubview(blurView, at: 0)にしており、Viewの背面にBlur処理を入れたViewが配置されるので、前面にする場合はaddSubview(blurView)に書き換えてください。
UIView+Extension.swift
extension UIView {
    
    func addBlur(style: UIBlurEffect.Style = .extraLight) {
        let blurView = UIVisualEffectView()
        blurView.effect = UIBlurEffect(style: style)
        blurView.frame = bounds
        blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        backgroundColor = .clear
        insertSubview(blurView, at: 0)
    }
    
    func removeBlur() {
        subviews
            .filter { $0.className == UIVisualEffectView.className }
            .forEach { $0.removeFromSuperview() }
    }
}

ViewControllerの背景にBlur処理を入れる

  • modalPresentationStyleを設定するのを忘れないようにしましょう
BlurViewController.swift
class BlurViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // BlurViewControllerがInitialViewControllerの場合
        self.modalPresentationStyle = .overCurrentContext // or .custom
        // BlurViewControllerはNavigationViewControllerのRootViewControllerの場合
        self.navigationController?.modalPresentationStyle = .overCurrentContext // or .custom
        // Blur処理
        self.view.addBlur()
    }
}
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?