mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/MastodonUI/View/Container/AdaptiveMarginContainerView...

62 lines
2.0 KiB
Swift
Raw Normal View History

2022-02-18 10:49:20 +01:00
//
// AdaptiveMarginContainerView.swift
//
//
// Created by MainasuK on 2022-2-18.
//
import UIKit
public final class AdaptiveMarginContainerView: UIView {
public var margin: CGFloat = 0 {
didSet { updateConstraints() }
}
public var contentView: UIView? {
didSet {
guard let contentView = contentView else { return }
guard contentView.superview == nil else { return }
contentView.translatesAutoresizingMaskIntoConstraints = false
addSubview(contentView)
let _topLayoutConstraint = contentView.topAnchor.constraint(equalTo: topAnchor)
2022-02-18 10:49:20 +01:00
let _leadingLayoutConstraint = contentView.leadingAnchor.constraint(equalTo: leadingAnchor)
let _trailingLayoutConstraint = trailingAnchor.constraint(equalTo: contentView.trailingAnchor)
let _bottomLayoutConstraint = bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
2022-02-18 10:49:20 +01:00
NSLayoutConstraint.activate([
_topLayoutConstraint,
2022-02-18 10:49:20 +01:00
_leadingLayoutConstraint,
_trailingLayoutConstraint,
_bottomLayoutConstraint
2022-02-18 10:49:20 +01:00
])
topLayoutConstraint = _topLayoutConstraint
2022-02-18 10:49:20 +01:00
leadingLayoutConstraint = _leadingLayoutConstraint
trailingLayoutConstraint = _trailingLayoutConstraint
bottomLayoutConstraint = _bottomLayoutConstraint
2022-02-18 10:49:20 +01:00
updateConstraints()
}
}
private(set) var topLayoutConstraint: NSLayoutConstraint?
private(set) var leadingLayoutConstraint: NSLayoutConstraint?
private(set) var trailingLayoutConstraint: NSLayoutConstraint?
private(set) var bottomLayoutConstraint: NSLayoutConstraint?
2022-02-18 10:49:20 +01:00
}
extension AdaptiveMarginContainerView {
public override func updateConstraints() {
super.updateConstraints()
leadingLayoutConstraint?.constant = margin
trailingLayoutConstraint?.constant = margin
}
}