Skip to content

Instantly share code, notes, and snippets.

@mohsinbmwm3
Created January 18, 2021 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohsinbmwm3/0c4eb8abe3624125f37d022dd58b4607 to your computer and use it in GitHub Desktop.
Save mohsinbmwm3/0c4eb8abe3624125f37d022dd58b4607 to your computer and use it in GitHub Desktop.
Some very useful UIView extension ideas.
import UIKit
extension UIView {
func addConstraintToLayoutSubviewInCenter(_ subview: UIView, size: CGSize? = nil) {
subview.translatesAutoresizingMaskIntoConstraints = false
subview.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
subview.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
if let _size = size {
subview.widthAnchor.constraint(equalToConstant: _size.width).isActive = true
subview.heightAnchor.constraint(equalToConstant: _size.height).isActive = true
}
}
func circular() {
layer.cornerRadius = frame.size.height / 2
clipsToBounds = true
}
func applyShadow(color: UIColor = .black, offset: CGSize = CGSize(width: 0.0, height: 1.0), opacity: Float = 0.2, radius: Float = 1.0) {
layer.shadowColor = color.cgColor
layer.shadowOffset = offset
layer.shadowOpacity = opacity
layer.shadowRadius = CGFloat(radius)
layer.masksToBounds = false
}
func applyGlow(color: UIColor) {
layer.shadowColor = color.cgColor
layer.shadowOffset = .zero
layer.shadowOpacity = 0.5
layer.shadowRadius = 10
layer.masksToBounds = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment