Constraints work
This commit is contained in:
parent
00af336298
commit
439217d0e1
|
@ -15,29 +15,25 @@ import UIKit
|
||||||
public final class LinkPreviewButton: UIControl {
|
public final class LinkPreviewButton: UIControl {
|
||||||
private var disposeBag = Set<AnyCancellable>()
|
private var disposeBag = Set<AnyCancellable>()
|
||||||
|
|
||||||
private let labelContainer = UIView()
|
private let containerStackView = UIStackView()
|
||||||
|
private let labelStackView = UIStackView()
|
||||||
|
|
||||||
private let highlightView = UIView()
|
private let highlightView = UIView()
|
||||||
private let imageView = UIImageView()
|
private let imageView = UIImageView()
|
||||||
private let titleLabel = UILabel()
|
private let titleLabel = UILabel()
|
||||||
private let linkLabel = UILabel()
|
private let linkLabel = UILabel()
|
||||||
|
|
||||||
private lazy var compactImageConstraints = [
|
private lazy var compactImageConstraints = [
|
||||||
labelContainer.topAnchor.constraint(greaterThanOrEqualTo: topAnchor),
|
|
||||||
labelContainer.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor),
|
|
||||||
labelContainer.centerYAnchor.constraint(equalTo: centerYAnchor),
|
|
||||||
labelContainer.leadingAnchor.constraint(equalTo: imageView.trailingAnchor),
|
|
||||||
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor),
|
|
||||||
imageView.heightAnchor.constraint(equalTo: heightAnchor),
|
imageView.heightAnchor.constraint(equalTo: heightAnchor),
|
||||||
|
imageView.widthAnchor.constraint(equalTo: heightAnchor),
|
||||||
heightAnchor.constraint(equalToConstant: 85),
|
heightAnchor.constraint(equalToConstant: 85),
|
||||||
]
|
]
|
||||||
|
|
||||||
private lazy var largeImageConstraints = [
|
private lazy var largeImageConstraints = [
|
||||||
labelContainer.topAnchor.constraint(equalTo: imageView.bottomAnchor),
|
imageView.heightAnchor.constraint(
|
||||||
labelContainer.bottomAnchor.constraint(equalTo: bottomAnchor),
|
equalTo: imageView.widthAnchor,
|
||||||
labelContainer.leadingAnchor.constraint(equalTo: leadingAnchor),
|
multiplier: 21 / 40
|
||||||
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: 21 / 40),
|
).priority(.defaultLow - 1),
|
||||||
imageView.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
||||||
imageView.widthAnchor.constraint(equalTo: widthAnchor),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
public override var isHighlighted: Bool {
|
public override var isHighlighted: Bool {
|
||||||
|
@ -73,43 +69,29 @@ public final class LinkPreviewButton: UIControl {
|
||||||
imageView.tintColor = Asset.Colors.Label.secondary.color
|
imageView.tintColor = Asset.Colors.Label.secondary.color
|
||||||
imageView.contentMode = .scaleAspectFill
|
imageView.contentMode = .scaleAspectFill
|
||||||
imageView.clipsToBounds = true
|
imageView.clipsToBounds = true
|
||||||
|
imageView.setContentHuggingPriority(.zero, for: .horizontal)
|
||||||
|
imageView.setContentHuggingPriority(.zero, for: .vertical)
|
||||||
|
imageView.setContentCompressionResistancePriority(.zero, for: .horizontal)
|
||||||
|
imageView.setContentCompressionResistancePriority(.zero, for: .vertical)
|
||||||
|
|
||||||
labelContainer.addSubview(titleLabel)
|
labelStackView.addArrangedSubview(titleLabel)
|
||||||
labelContainer.addSubview(linkLabel)
|
labelStackView.addArrangedSubview(linkLabel)
|
||||||
labelContainer.layoutMargins = .init(top: 10, left: 10, bottom: 10, right: 10)
|
labelStackView.layoutMargins = .init(top: 10, left: 10, bottom: 10, right: 10)
|
||||||
|
labelStackView.isLayoutMarginsRelativeArrangement = true
|
||||||
|
labelStackView.axis = .vertical
|
||||||
|
|
||||||
addSubview(imageView)
|
containerStackView.addArrangedSubview(imageView)
|
||||||
addSubview(labelContainer)
|
containerStackView.addArrangedSubview(labelStackView)
|
||||||
|
containerStackView.isUserInteractionEnabled = false
|
||||||
|
|
||||||
|
addSubview(containerStackView)
|
||||||
addSubview(highlightView)
|
addSubview(highlightView)
|
||||||
|
|
||||||
subviews.forEach { $0.isUserInteractionEnabled = false }
|
containerStackView.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
|
||||||
labelContainer.translatesAutoresizingMaskIntoConstraints = false
|
|
||||||
titleLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
||||||
linkLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
||||||
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
||||||
highlightView.translatesAutoresizingMaskIntoConstraints = false
|
highlightView.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
|
||||||
NSLayoutConstraint.activate([
|
containerStackView.pinToParent()
|
||||||
titleLabel.topAnchor.constraint(equalTo: labelContainer.layoutMarginsGuide.topAnchor),
|
highlightView.pinToParent()
|
||||||
titleLabel.leadingAnchor.constraint(equalTo: labelContainer.layoutMarginsGuide.leadingAnchor),
|
|
||||||
titleLabel.trailingAnchor.constraint(equalTo: labelContainer.layoutMarginsGuide.trailingAnchor),
|
|
||||||
|
|
||||||
linkLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 2),
|
|
||||||
linkLabel.bottomAnchor.constraint(equalTo: labelContainer.layoutMarginsGuide.bottomAnchor),
|
|
||||||
linkLabel.leadingAnchor.constraint(equalTo: labelContainer.layoutMarginsGuide.leadingAnchor),
|
|
||||||
linkLabel.trailingAnchor.constraint(equalTo: labelContainer.layoutMarginsGuide.trailingAnchor),
|
|
||||||
|
|
||||||
labelContainer.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
||||||
|
|
||||||
imageView.topAnchor.constraint(equalTo: topAnchor),
|
|
||||||
imageView.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
||||||
|
|
||||||
highlightView.topAnchor.constraint(equalTo: topAnchor),
|
|
||||||
highlightView.bottomAnchor.constraint(equalTo: bottomAnchor),
|
|
||||||
highlightView.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
||||||
highlightView.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
||||||
])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
|
@ -129,19 +111,24 @@ public final class LinkPreviewButton: UIControl {
|
||||||
) { [weak imageView] image, _, _, _ in
|
) { [weak imageView] image, _, _, _ in
|
||||||
if image != nil {
|
if image != nil {
|
||||||
imageView?.contentMode = .scaleAspectFill
|
imageView?.contentMode = .scaleAspectFill
|
||||||
|
self.containerStackView.setNeedsLayout()
|
||||||
|
self.containerStackView.layoutIfNeeded()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NSLayoutConstraint.deactivate(compactImageConstraints + largeImageConstraints)
|
NSLayoutConstraint.deactivate(compactImageConstraints + largeImageConstraints)
|
||||||
|
|
||||||
if isCompact {
|
if isCompact {
|
||||||
|
containerStackView.alignment = .center
|
||||||
|
containerStackView.axis = .horizontal
|
||||||
|
containerStackView.distribution = .fill
|
||||||
NSLayoutConstraint.activate(compactImageConstraints)
|
NSLayoutConstraint.activate(compactImageConstraints)
|
||||||
} else {
|
} else {
|
||||||
|
containerStackView.alignment = .fill
|
||||||
|
containerStackView.axis = .vertical
|
||||||
|
containerStackView.distribution = .equalSpacing
|
||||||
NSLayoutConstraint.activate(largeImageConstraints)
|
NSLayoutConstraint.activate(largeImageConstraints)
|
||||||
}
|
}
|
||||||
|
|
||||||
setNeedsLayout()
|
|
||||||
layoutIfNeeded()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override func didMoveToWindow() {
|
public override func didMoveToWindow() {
|
||||||
|
@ -166,3 +153,7 @@ public final class LinkPreviewButton: UIControl {
|
||||||
imageView.backgroundColor = theme.systemElevatedBackgroundColor
|
imageView.backgroundColor = theme.systemElevatedBackgroundColor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private extension UILayoutPriority {
|
||||||
|
static let zero = UILayoutPriority(rawValue: 0)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue