-
-
Save seanlilmateus/e62bfaad97470250dd08efb4738f339d to your computer and use it in GitHub Desktop.
Creates a UIViewController with Blurred Background Which acts like a UIHostingController for SwiftUI Views (Useful for UIKit developers using SwiftUI Views for Modal Views)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BluredBackgroundHostingController.swift | |
// v1.0.1 | |
// | |
// Created by Ashley Chapman on 07/04/2020. | |
// Copyright © 2020 Ashley Chapman. All rights reserved. | |
// | |
import UIKit | |
class BlurredHostingController: UIViewController { | |
// Variables | |
var hostingController: UIViewController! | |
var blurEffect: UIBlurEffect.Style = .prominent | |
var translucentEffect: TranslucentEffect = .regular | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
self.view.backgroundColor = nil | |
if (translucentEffect != .opaque) { | |
let blurEffect = UIBlurEffect(style: self.blurEffect) | |
let blurEffectView = UIVisualEffectView(effect: blurEffect) | |
blurEffectView.frame = self.view.frame | |
self.view.addSubview(blurEffectView) | |
NSLayoutConstraint.activate([ | |
blurEffectView.widthAnchor.constraint(equalTo: view.widthAnchor), | |
blurEffectView.heightAnchor.constraint(equalTo: view.heightAnchor), | |
blurEffectView.centerXAnchor.constraint(equalTo: view.centerXAnchor), | |
blurEffectView.centerYAnchor.constraint(equalTo: view.centerYAnchor) | |
]) | |
} | |
self.addChild(hostingController) | |
hostingController.view.translatesAutoresizingMaskIntoConstraints = false | |
hostingController.view.alpha = translucent() | |
hostingController.view.backgroundColor = nil | |
self.view.addSubview(hostingController.view) | |
hostingController.didMove(toParent: self) | |
NSLayoutConstraint.activate([ | |
hostingController.view.widthAnchor.constraint(equalTo: view.widthAnchor), | |
hostingController.view.heightAnchor.constraint(equalTo: view.heightAnchor), | |
hostingController.view.centerXAnchor.constraint(equalTo: view.centerXAnchor), | |
hostingController.view.centerYAnchor.constraint(equalTo: view.centerYAnchor) | |
]) | |
} | |
private func translucent() -> CGFloat { | |
if (translucentEffect == .opaque) { | |
return 1.0 | |
} else if (translucentEffect == .ultrathick) { | |
return 0.95 | |
} else if (translucentEffect == .thick) { | |
return 0.9 | |
} else if (translucentEffect == .regular) { | |
return 0.85 | |
} else if (translucentEffect == .prominent) { | |
return 0.8 | |
} else if (translucentEffect == .thin) { | |
return 0.75 | |
} else if (translucentEffect == .ultrathin) { | |
return 0.7 | |
} else { | |
return 1.0 | |
} | |
} | |
} | |
enum TranslucentEffect { | |
case opaque | |
case ultrathick | |
case thick | |
case regular | |
case prominent | |
case thin | |
case ultrathin | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment