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)
|
|
|
|
|
2022-02-18 11:25:26 +01:00
|
|
|
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)
|
2022-02-18 11:25:26 +01:00
|
|
|
let _bottomLayoutConstraint = bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
|
2022-02-18 10:49:20 +01:00
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
2022-02-18 11:25:26 +01:00
|
|
|
_topLayoutConstraint,
|
2022-02-18 10:49:20 +01:00
|
|
|
_leadingLayoutConstraint,
|
|
|
|
_trailingLayoutConstraint,
|
2022-02-18 11:25:26 +01:00
|
|
|
_bottomLayoutConstraint
|
2022-02-18 10:49:20 +01:00
|
|
|
])
|
|
|
|
|
2022-02-18 11:25:26 +01:00
|
|
|
topLayoutConstraint = _topLayoutConstraint
|
2022-02-18 10:49:20 +01:00
|
|
|
leadingLayoutConstraint = _leadingLayoutConstraint
|
|
|
|
trailingLayoutConstraint = _trailingLayoutConstraint
|
2022-02-18 11:25:26 +01:00
|
|
|
bottomLayoutConstraint = _bottomLayoutConstraint
|
2022-02-18 10:49:20 +01:00
|
|
|
|
|
|
|
updateConstraints()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 11:25:26 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|