Skip to content

Instantly share code, notes, and snippets.

@yosshi4486
Created August 30, 2021 01:08
Show Gist options
  • Select an option

  • Save yosshi4486/2c7c22f0d30066dcfd8646ef66012ce0 to your computer and use it in GitHub Desktop.

Select an option

Save yosshi4486/2c7c22f0d30066dcfd8646ef66012ce0 to your computer and use it in GitHub Desktop.
Subtitled navigation title view
class SubtitledNavigationTitleView: UIView {
let titleLabel: UILabel = {
let label = UILabel()
label.adjustsFontForContentSizeCategory = true
label.font = .preferredFont(forTextStyle: .headline)
label.textAlignment = .center
label.numberOfLines = 1
return label
}()
let subtitleLabel: UILabel = {
let label = UILabel()
label.adjustsFontForContentSizeCategory = true
label.font = .preferredFont(forTextStyle: .caption1)
label.lineBreakMode = .byTruncatingTail
label.textAlignment = .center
label.textColor = .secondaryLabel
label.numberOfLines = 1
return label
}()
override var intrinsicContentSize: CGSize {
return UIView.layoutFittingCompressedSize
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setup() {
addSubview(titleLabel)
addSubview(subtitleLabel)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
titleLabel.topAnchor.constraint(equalTo: topAnchor),
titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor),
titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor),
subtitleLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor),
subtitleLabel.leadingAnchor.constraint(equalTo: titleLabel.leadingAnchor),
subtitleLabel.trailingAnchor.constraint(equalTo: titleLabel.trailingAnchor),
subtitleLabel.bottomAnchor.constraint(equalTo: bottomAnchor)
])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment