Last active
March 7, 2024 20:10
-
-
Save maxhand/24f9a69dc83705cfc025cae63f86a03c to your computer and use it in GitHub Desktop.
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
import UIKit | |
class BackgroundView: UIView { | |
private let containedView = UIStackView() | |
private var buttonAction: (() -> ())? | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
containedView.alignment = .center | |
containedView.axis = .vertical | |
containedView.spacing = 4 | |
setupView() | |
} | |
private func setupView() { | |
addSubview(containedView) | |
containedView.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
containedView.centerXAnchor.constraint(equalTo: centerXAnchor), | |
containedView.centerYAnchor.constraint(equalTo: centerYAnchor), | |
containedView.widthAnchor.constraint(equalToConstant: 250) | |
]) | |
} | |
func configure(using representable: ListBackgroundViewRepresentable?) { | |
guard let representable else { return } | |
let symbolConfiguration = UIImage.SymbolConfiguration(scale: .large) | |
let imageView = UIImageView(image: UIImage(systemName: representable.systemImageName)?.withConfiguration(symbolConfiguration).withTintColor(.systemGray2).withRenderingMode(.alwaysOriginal)) | |
let titleLabel = UILabel() | |
titleLabel.font = .preferredFont(forTextStyle: .headline) | |
titleLabel.textColor = .systemGray | |
titleLabel.adjustsFontForContentSizeCategory = true | |
titleLabel.numberOfLines = 0 | |
titleLabel.text = representable.title | |
let secondaryLabel = UILabel() | |
secondaryLabel.font = .preferredFont(forTextStyle: .body) | |
secondaryLabel.text = representable.body | |
secondaryLabel.numberOfLines = 0 | |
secondaryLabel.textColor = .systemGray2 | |
secondaryLabel.textAlignment = .center | |
let accessoryButton = UIButton() | |
var accessoryButtonConfig: UIButton.Configuration = .filled() | |
accessoryButtonConfig.title = representable.buttonTitle | |
accessoryButtonConfig.cornerStyle = .capsule | |
accessoryButton.layer.cornerRadius = 15 | |
accessoryButton.addTarget(self, action: #selector(handleButtonPressed), for: .touchUpInside) | |
accessoryButton.configuration = accessoryButtonConfig | |
accessoryButton.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
accessoryButton.widthAnchor.constraint(equalToConstant: 110), | |
accessoryButton.heightAnchor.constraint(equalToConstant: 35) | |
]) | |
var views = [imageView, titleLabel, secondaryLabel] | |
if representable.buttonAction != nil && representable.buttonTitle != nil { | |
views.append(accessoryButton) | |
} | |
buttonAction = representable.buttonAction | |
for view in views { | |
containedView.addArrangedSubview(view) | |
} | |
containedView.setCustomSpacing(8, after: imageView) | |
containedView.setCustomSpacing(20, after: secondaryLabel) | |
} | |
@objc private func handleButtonPressed(sender: UIButton) { | |
buttonAction?() | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} |
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
let backgroundView = BackgroundView() | |
backgroundView.configure( | |
using: ListBackgroundView( | |
systemImageName: "line.3.horizontal.decrease", | |
title: "These are not the results that you are looking for", | |
buttonTitle: "Energize", | |
buttonAction: { | |
// Do great things | |
} | |
) | |
) |
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
struct ListBackgroundView: ListBackgroundViewRepresentable { | |
var systemImageName: String | |
var title: String | |
var body: String? | |
var buttonTitle: String? | |
var buttonAction: (() -> ())? | |
} |
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
protocol ListBackgroundViewRepresentable { | |
var systemImageName: String { get } | |
var title: String { get } | |
var body: String? { get } | |
var buttonTitle: String? { get } | |
var buttonAction: (() -> ())? { get } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Look at this beauty!!! Thanks Max. I owe you one.