diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2023-05-16 13:16:13 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2023-05-30 11:17:29 +0200 |
| commit | 95f798bd2bba1a89722863564c65076417df500f (patch) | |
| tree | d5f8a6176e6b2596ec26f9a0c60969d765ce5a6f | |
| parent | 44954fb5291a724b3ac4bd4ebaeb6bce78713a2b (diff) | |
| download | mullvadvpn-95f798bd2bba1a89722863564c65076417df500f.tar.xz mullvadvpn-95f798bd2bba1a89722863564c65076417df500f.zip | |
NotificationBanner: hide action button when it's not provided
| -rw-r--r-- | ios/MullvadVPN/Extensions/NSLayoutConstraint+Helpers.swift | 1 | ||||
| -rw-r--r-- | ios/MullvadVPN/Notifications/UI/NotificationBannerView.swift | 78 |
2 files changed, 35 insertions, 44 deletions
diff --git a/ios/MullvadVPN/Extensions/NSLayoutConstraint+Helpers.swift b/ios/MullvadVPN/Extensions/NSLayoutConstraint+Helpers.swift index 3a41101a96..7301c17aa4 100644 --- a/ios/MullvadVPN/Extensions/NSLayoutConstraint+Helpers.swift +++ b/ios/MullvadVPN/Extensions/NSLayoutConstraint+Helpers.swift @@ -9,6 +9,7 @@ import UIKit extension NSLayoutConstraint { + /// Sets constraint priority and returns `self` func withPriority(_ priority: UILayoutPriority) -> Self { self.priority = priority return self diff --git a/ios/MullvadVPN/Notifications/UI/NotificationBannerView.swift b/ios/MullvadVPN/Notifications/UI/NotificationBannerView.swift index 099da9187f..07d969cd62 100644 --- a/ios/MullvadVPN/Notifications/UI/NotificationBannerView.swift +++ b/ios/MullvadVPN/Notifications/UI/NotificationBannerView.swift @@ -12,16 +12,10 @@ final class NotificationBannerView: UIView { private static let indicatorViewSize = CGSize(width: 12, height: 12) private static let buttonSize = CGSize(width: 18, height: 18) - private let backgroundView: UIVisualEffectView = { - let effect = UIBlurEffect(style: .dark) - let visualEffectView = UIVisualEffectView(effect: effect) - visualEffectView.translatesAutoresizingMaskIntoConstraints = false - return visualEffectView - }() + private let backgroundView = UIVisualEffectView(effect: UIBlurEffect(style: .dark)) private let titleLabel: UILabel = { let textLabel = UILabel() - textLabel.translatesAutoresizingMaskIntoConstraints = false textLabel.font = UIFont.systemFont(ofSize: 17, weight: .bold) textLabel.textColor = UIColor.InAppNotificationBanner.titleColor textLabel.numberOfLines = 0 @@ -35,7 +29,6 @@ final class NotificationBannerView: UIView { private let bodyLabel: UILabel = { let textLabel = UILabel() - textLabel.translatesAutoresizingMaskIntoConstraints = false textLabel.font = UIFont.systemFont(ofSize: 17) textLabel.textColor = UIColor.InAppNotificationBanner.bodyColor textLabel.numberOfLines = 0 @@ -49,7 +42,6 @@ final class NotificationBannerView: UIView { private let indicatorView: UIView = { let view = UIView() - view.translatesAutoresizingMaskIntoConstraints = false view.backgroundColor = .dangerColor view.layer.cornerRadius = NotificationBannerView.indicatorViewSize.width * 0.5 view.layer.cornerCurve = .circular @@ -58,15 +50,20 @@ final class NotificationBannerView: UIView { private let wrapperView: UIView = { let view = UIView() - view.translatesAutoresizingMaskIntoConstraints = false view.directionalLayoutMargins = UIMetrics.inAppBannerNotificationLayoutMargins return view }() + private let bodyStackView: UIStackView = { + let stackView = UIStackView() + stackView.alignment = .top + stackView.distribution = .fill + return stackView + }() + private let actionButton: IncreasedHitButton = { - let button = IncreasedHitButton() + let button = IncreasedHitButton(type: .system) button.tintColor = UIColor.InAppNotificationBanner.actionButtonColor - button.translatesAutoresizingMaskIntoConstraints = false return button }() @@ -90,37 +87,38 @@ final class NotificationBannerView: UIView { var action: InAppNotificationAction? { didSet { - actionButton.setImage(action?.image, for: .normal) + let image = action?.image + let showsAction = image != nil + + actionButton.setImage(image, for: .normal) + actionButton.isHidden = !showsAction } } override init(frame: CGRect) { super.init(frame: frame) - for subview in [titleLabel, bodyLabel, indicatorView, actionButton] { - wrapperView.addSubview(subview) - } - - backgroundView.contentView.addSubview(wrapperView) - addSubview(backgroundView) - actionButton.addTarget(self, action: #selector(didPress), for: .touchUpInside) - NSLayoutConstraint.activate([ - backgroundView.topAnchor.constraint(equalTo: topAnchor), - backgroundView.leadingAnchor.constraint(equalTo: leadingAnchor), - backgroundView.trailingAnchor.constraint(equalTo: trailingAnchor), - backgroundView.bottomAnchor.constraint(equalTo: bottomAnchor), + actionButton.setContentCompressionResistancePriority(.defaultHigh + 1, for: .horizontal) + actionButton.setContentCompressionResistancePriority(.defaultHigh + 1, for: .vertical) + actionButton.setContentHuggingPriority(.defaultHigh + 1, for: .horizontal) + actionButton.setContentHuggingPriority(.defaultHigh + 1, for: .vertical) + + wrapperView.addConstrainedSubviews([titleLabel, indicatorView, bodyStackView]) + backgroundView.contentView.addConstrainedSubviews([wrapperView]) { + wrapperView.pinEdgesToSuperview() + } + addConstrainedSubviews([backgroundView]) { + backgroundView.pinEdgesToSuperview() + } - wrapperView.topAnchor.constraint(equalTo: backgroundView.contentView.topAnchor), - wrapperView.leadingAnchor.constraint(equalTo: backgroundView.contentView.leadingAnchor), - wrapperView.trailingAnchor - .constraint(equalTo: backgroundView.contentView.trailingAnchor), - wrapperView.bottomAnchor.constraint(equalTo: backgroundView.contentView.bottomAnchor), + bodyStackView.addArrangedSubview(bodyLabel) + bodyStackView.addArrangedSubview(actionButton) + NSLayoutConstraint.activate([ indicatorView.bottomAnchor.constraint(equalTo: titleLabel.firstBaselineAnchor), - indicatorView.leadingAnchor - .constraint(equalTo: wrapperView.layoutMarginsGuide.leadingAnchor), + indicatorView.leadingAnchor.constraint(equalTo: wrapperView.layoutMarginsGuide.leadingAnchor), indicatorView.widthAnchor.constraint(equalToConstant: Self.indicatorViewSize.width), indicatorView.heightAnchor.constraint(equalToConstant: Self.indicatorViewSize.height), @@ -128,18 +126,10 @@ final class NotificationBannerView: UIView { titleLabel.leadingAnchor.constraint(equalToSystemSpacingAfter: indicatorView.trailingAnchor, multiplier: 1), titleLabel.trailingAnchor.constraint(equalTo: wrapperView.layoutMarginsGuide.trailingAnchor), - bodyLabel.topAnchor.constraint( - equalToSystemSpacingBelow: titleLabel.bottomAnchor, - multiplier: 1 - ), - bodyLabel.leadingAnchor.constraint(equalTo: titleLabel.leadingAnchor), - bodyLabel.bottomAnchor.constraint(equalTo: wrapperView.layoutMarginsGuide.bottomAnchor), - - actionButton.leadingAnchor.constraint(equalTo: bodyLabel.trailingAnchor), - actionButton.topAnchor.constraint(equalTo: bodyLabel.topAnchor), - actionButton.trailingAnchor.constraint(equalTo: wrapperView.layoutMarginsGuide.trailingAnchor), - actionButton.widthAnchor.constraint(equalToConstant: NotificationBannerView.buttonSize.width), - actionButton.heightAnchor.constraint(equalToConstant: NotificationBannerView.buttonSize.height), + bodyStackView.topAnchor.constraint(equalToSystemSpacingBelow: titleLabel.bottomAnchor, multiplier: 1), + bodyStackView.leadingAnchor.constraint(equalTo: titleLabel.leadingAnchor), + bodyStackView.trailingAnchor.constraint(equalTo: titleLabel.trailingAnchor), + bodyStackView.bottomAnchor.constraint(equalTo: wrapperView.layoutMarginsGuide.bottomAnchor), ]) } |
