Last active
December 20, 2021 21:31
-
-
Save thomsmed/c26481b8fc201afb931d6fd710e02b09 to your computer and use it in GitHub Desktop.
OctocatTableHeaderView.swift - From "Stretchy table view header" @ medium.com
This file contains hidden or 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
| // | |
| // OctocatTableHeaderView.swift | |
| // | |
| import Foundation | |
| import UIKit | |
| class OctocatTableHeaderView: StretchyTableHeaderView { | |
| // UITableView.tableHeaderView needs a defined height: https://developer.apple.com/documentation/uikit/uitableview/1614904-tableheaderview | |
| static let baseHeight: CGFloat = 250 | |
| private let imageView: UIImageView = { | |
| let imageView = UIImageView(image: UIImage(named: "octocats")!) | |
| imageView.contentMode = .scaleAspectFill | |
| return imageView | |
| }() | |
| private let titleLabel: UILabel = { | |
| let label = UILabel() | |
| label.text = "This is some bananas" | |
| return label | |
| }() | |
| private let titleView: UIView = { | |
| let blurEffect = UIBlurEffect(style: .systemThinMaterial) | |
| let blurVisualEffectView = UIVisualEffectView(effect: blurEffect) | |
| blurVisualEffectView.layer.cornerRadius = 16 | |
| blurVisualEffectView.clipsToBounds = true | |
| let vibrancyVisualEffectView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: blurEffect)) | |
| let label = UILabel() | |
| label.font = .systemFont(ofSize: 20, weight: .heavy) | |
| label.text = "Octocat is the best <3" | |
| blurVisualEffectView.contentView.addSubview(vibrancyVisualEffectView) | |
| vibrancyVisualEffectView.contentView.addSubview(label) | |
| vibrancyVisualEffectView.edgesToSuperview() | |
| label.edgesToSuperview(insets: .uniform(16)) | |
| return blurVisualEffectView | |
| }() | |
| override init(frame: CGRect) { | |
| super.init(frame: frame) | |
| configureUI() | |
| } | |
| required init?(coder: NSCoder) { | |
| super.init(coder: coder) | |
| configureUI() | |
| } | |
| private func configureUI() { | |
| contentView.addSubview(imageView) | |
| contentView.addSubview(titleView) | |
| imageView.edgesToSuperview() | |
| titleView.leadingToSuperview(offset: 16, usingSafeArea: true) | |
| titleView.bottomToSuperview(offset: -16, usingSafeArea: true) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment