UIOverlayView is a subclass of UIView used to achieve application gray screen effect.(UIOverlayView是一个UIView的子类,用以实现应用灰屏效果。)
To run the example project, clone the repo, and run pod install
from the Example directory first.
- iOS 12.0 or later
- Swift 5.9.2
- Xcode 15.1
UIOverlayView is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'UIOverlayView'
If you want to use the latest features of UIOverlayView use normal external source dependencies.
pod 'UIOverlayView', :git => 'https://github.com/Sfh03031/UIOverlayView.git'
First, install and import UIOverlayView
import UIOverlayView
Second, use class methods of UIOverlayView where you needed to show or hide
# show overlay
UIOverlayView.show()
# hide overlay
UIOverlayView.hide()
It's that simple, enjoy it.
UIOverlayView also provides 16 rendering modes in the form of enumeration, Like this:
public enum UIOverlayViewBlendMode: String, CaseIterable {
case normalBlendMode = "normalBlendMode"
case darkenBlendMode = "darkenBlendMode"
case multiplyBlendMode = "multiplyBlendMode"
case colorBurnBlendMode = "colorBurnBlendMode"
case lightenBlendMode = "lightenBlendMode"
case screenBlendMode = "screenBlendMode"
case colorDodgeBlendMode = "colorDodgeBlendMode"
case overlayBlendMode = "overlayBlendMode"
case softLightBlendMode = "softLightBlendMode"
case hardLightBlendMode = "hardLightBlendMode"
case differenceBlendMode = "differenceBlendMode"
case exclusionBlendMode = "exclusionBlendMode"
case hueBlendMode = "hueBlendMode"
case saturationBlendMode = "saturationBlendMode"
case colorBlendMode = "colorBlendMode"
case luminosityBlendMode = "luminosityBlendMode"
}
and default mode is saturationBlendMode
, default background color is lightGray
, You can try to customize the effect you want with different combinations,UIOverlayView alse offers instance setting methods for rendering modes and background colors for easy customization, like this:
public extension UIOverlayView {
/// set compositingFilter of layer
/// - Parameter blendMode: case of UIOverlayViewBlendMode
/// - Returns: Self
func blend(mode blendMode: UIOverlayViewBlendMode) -> Self {
self.layer.compositingFilter = blendMode.rawValue
return self
}
/// set background color
/// - Parameter kcolor: color
/// - Returns: Self
func background(color kcolor: UIColor) -> Self {
self.backgroundColor = kcolor
return self
}
/// add overlay
/// - Parameter container: superView
func overlay(in container: UIView) {
self.frame = container.bounds
container.addSubview(self)
}
/// remove overlay
func remove() {
self.removeFromSuperview()
}
}
The superView of UIOverlayView defaults to keyWindow
, but you can also customize it.
public extension UIOverlayView {
/// show overlay
/// - Parameters:
/// - container: superView of UIOverlayView
/// - mode: blendMode of UIOverlayView
/// - backColor: backgroundColor of UIOverlayView
static func show(in container: UIView? = UIApplication.shared.windows.first(where: { $0.isKeyWindow }), mode: UIOverlayViewBlendMode = .saturationBlendMode, backColor: UIColor = .lightGray) {
if let contain = container {
if let exist = contain.subviews.first(where: { $0 is UIOverlayView }) {
exist.removeFromSuperview()
}
UIOverlayView().blend(mode: mode).background(color: backColor).overlay(in: contain)
}
}
/// hide overlay
/// - Parameter container: superView of UIOverlayView
static func hide(in container: UIView? = UIApplication.shared.windows.first(where: { $0.isKeyWindow })) {
if let contain = container {
if let exist = contain.subviews.first(where: { $0 is UIOverlayView }) {
exist.removeFromSuperview()
}
}
}
}
2025.04.1, 0.1.1
- Initial version(zh: 初始版本)
Sfh03031, [email protected]
UIOverlayView is available under the MIT license. See the LICENSE file for more info.