Last active
October 3, 2018 06:43
-
-
Save bok-/aab0d270891e41add051b3b6af016607 to your computer and use it in GitHub Desktop.
Slap this baby against an edge and let it extent the visual effect below the safe area, but not the content view.
This file contains 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
// | |
// SafeVisualEffectView.swift | |
// Created by Rob Amos 2018-08-19. | |
// | |
import UIKit | |
open class SafeVisualEffectView: UIVisualEffectView { | |
// MARK: Initialisation | |
public init(effect: UIVisualEffect?, extendableEdges: UIRectEdge = .all) { | |
super.init(effect: effect) | |
self.setup(edges: extendableEdges) | |
} | |
public required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
self.setup(edges: .all) | |
} | |
// MARK: - Setup and Layout | |
private func setup (edges: UIRectEdge) { | |
guard #available(iOS 11.0, *) else { return } | |
self.contentView.removeConstraints(self.contentView.constraints) | |
let constraints = [ | |
self.contentView.topAnchor.constraint(equalTo: edges.contains(.top) ? self.safeAreaLayoutGuide.topAnchor : self.topAnchor), | |
self.contentView.rightAnchor.constraint(equalTo: edges.contains(.right) ? self.safeAreaLayoutGuide.rightAnchor : self.rightAnchor), | |
self.contentView.bottomAnchor.constraint(equalTo: edges.contains(.bottom) ? self.safeAreaLayoutGuide.bottomAnchor : self.bottomAnchor), | |
self.contentView.leftAnchor.constraint(equalTo: edges.contains(.left) ? self.safeAreaLayoutGuide.leftAnchor : self.leftAnchor), | |
self.widthAnchor.constraint(greaterThanOrEqualTo: self.contentView.widthAnchor, multiplier: 1), | |
self.heightAnchor.constraint(greaterThanOrEqualTo: self.contentView.heightAnchor, multiplier: 1), | |
] | |
NSLayoutConstraint.activate(constraints) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment