Skip to content

Instantly share code, notes, and snippets.

@gromwel
Created April 12, 2021 11:11
Show Gist options
  • Save gromwel/4b7be7253c27ff5460e94a64e22b5c0a to your computer and use it in GitHub Desktop.
Save gromwel/4b7be7253c27ff5460e94a64e22b5c0a to your computer and use it in GitHub Desktop.
Single view accessory view
final class SingleViewAccessory: UIView {
// MARK: - Контент
private let contentView: UIView
// MARK: - Инициализация
init(content: UIView, background: UIColor = .clear) {
// Контент
self.contentView = content
// Инициализация
super.init(frame: .zero)
// Сабвью
self.addSubview(self.contentView)
// Настройка
self.autoresizingMask = .flexibleHeight
self.backgroundColor = background
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError() }
// MARK: - Размеры
override var intrinsicContentSize: CGSize {
CGSize(
width: UIScreen.main.bounds.width,
height: self.contentView.sizeThatFits(UIScreen.main.bounds.size).height + self.safeAreaInsets.bottom
)
}
override func layoutSubviews() {
super.layoutSubviews()
// Размеры
let size: CGSize = self.frame.size
let height: CGFloat = ceil(self.contentView.sizeThatFits(size).height)
// Расстановка
self.contentView.frame = CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: height)
}
// MARK: - Цикл жизни
override func safeAreaInsetsDidChange() {
super.safeAreaInsetsDidChange()
self.invalidateIntrinsicContentSize()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment