Files
eqn.ios/Sources/Earthquake Network/UI/EQNBlurredCloseButton.swift
T

77 lines
2.3 KiB
Swift

//
// EQNBlurredCloseButton.swift
// Earthquake Network
//
// Created by Andrea Busi on 07/03/21.
// Copyright © 2021 Earthquake Network. All rights reserved.
//
import Foundation
import UIKit
class EQNBlurredCloseButton: UIButton {
// MARK: - Init
convenience init() {
self.init(type: .custom)
setupUI()
}
override func awakeFromNib() {
super.awakeFromNib()
setupUI()
}
// MARK: - Private
private func setupUI() {
layer.masksToBounds = true
titleLabel?.font = UIFont.monospacedDigitSystemFont(ofSize: 16.0, weight: .medium)
setTitle("X", for: .normal)
addBlurEffect(style: .extraLight)
}
private func addBlurEffect(style: UIBlurEffect.Style = .regular) {
backgroundColor = .clear
let blurView = UIVisualEffectView(effect: UIBlurEffect(style: style))
blurView.isUserInteractionEnabled = false
blurView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.6)
insertSubview(blurView, at: 0)
blurView.translatesAutoresizingMaskIntoConstraints = false
leadingAnchor.constraint(equalTo: blurView.leadingAnchor).isActive = true
trailingAnchor.constraint(equalTo: blurView.trailingAnchor).isActive = true
topAnchor.constraint(equalTo: blurView.topAnchor).isActive = true
bottomAnchor.constraint(equalTo: blurView.bottomAnchor).isActive = true
if let imageView = self.imageView {
imageView.backgroundColor = .clear
bringSubviewToFront(imageView)
}
}
override func layoutSubviews() {
super.layoutSubviews()
// force the button to be circle
layer.cornerRadius = frame.height / 2.0
}
// MARK: - Public
/// Add constaints to show the button on the upper right corner
func addDefaultConstraint(to view: UIView) {
trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10.0).isActive = true
topAnchor.constraint(equalTo: view.topAnchor, constant: 10.0).isActive = true
widthAnchor.constraint(equalTo: heightAnchor).isActive = true
heightAnchor.constraint(equalToConstant: 34.0).isActive = true
}
}