mastodon-ios/Mastodon/Scene/Onboarding/Share/NavigationActionView.swift

110 lines
4.3 KiB
Swift
Raw Normal View History

2022-01-04 11:30:21 +01:00
//
// NavigationActionView.swift
// Mastodon
//
// Created by MainasuK on 2021-12-31.
//
import UIKit
2022-01-05 08:11:35 +01:00
import MastodonUI
import MastodonAsset
import MastodonLocalization
2022-01-04 11:30:21 +01:00
final class NavigationActionView: UIView {
static let buttonHeight: CGFloat = 50
static let minimumBackButtonWidth: CGFloat = 100
2022-01-04 11:30:21 +01:00
2022-01-05 08:11:35 +01:00
private var observations = Set<NSKeyValueObservation>()
2022-01-04 11:30:21 +01:00
let buttonContainer: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.spacing = 18
return stackView
}()
2022-01-05 08:11:35 +01:00
let backButtonShadowContainer = ShadowBackgroundContainer()
2022-01-04 11:30:21 +01:00
let backButton: PrimaryActionButton = {
let button = PrimaryActionButton()
button.action = .back
button.contentEdgeInsets = WelcomeViewController.actionButtonPadding
button.titleLabel?.adjustsFontForContentSizeCategory = true
2022-01-04 11:30:21 +01:00
button.setTitle(L10n.Common.Controls.Actions.back, for: .normal)
return button
}()
2022-01-05 08:11:35 +01:00
let nextButtonShadowContainer = ShadowBackgroundContainer()
2022-01-04 11:30:21 +01:00
let nextButton: PrimaryActionButton = {
let button = PrimaryActionButton()
button.action = .next
button.contentEdgeInsets = WelcomeViewController.actionButtonPadding
button.titleLabel?.adjustsFontForContentSizeCategory = true
2022-01-04 11:30:21 +01:00
button.setTitle(L10n.Common.Controls.Actions.next, for: .normal)
return button
}()
2022-02-08 05:36:06 +01:00
var hidesBackButton: Bool = false {
didSet { backButtonShadowContainer.isHidden = hidesBackButton }
}
var hidesNextButton: Bool = false {
didSet { nextButtonShadowContainer.isHidden = hidesNextButton }
}
2022-01-04 11:30:21 +01:00
override init(frame: CGRect) {
super.init(frame: frame)
_init()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
_init()
}
}
extension NavigationActionView {
2022-02-08 05:36:06 +01:00
2022-01-04 11:30:21 +01:00
private func _init() {
buttonContainer.translatesAutoresizingMaskIntoConstraints = false
buttonContainer.preservesSuperviewLayoutMargins = true
addSubview(buttonContainer)
NSLayoutConstraint.activate([
buttonContainer.topAnchor.constraint(equalTo: topAnchor, constant: 16),
buttonContainer.leadingAnchor.constraint(equalTo: readableContentGuide.leadingAnchor),
buttonContainer.trailingAnchor.constraint(equalTo: readableContentGuide.trailingAnchor),
safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: buttonContainer.bottomAnchor, constant: 8),
])
2022-01-05 08:11:35 +01:00
backButtonShadowContainer.translatesAutoresizingMaskIntoConstraints = false
buttonContainer.addArrangedSubview(backButtonShadowContainer)
nextButtonShadowContainer.translatesAutoresizingMaskIntoConstraints = false
buttonContainer.addArrangedSubview(nextButtonShadowContainer)
NSLayoutConstraint.activate([
backButtonShadowContainer.heightAnchor.constraint(greaterThanOrEqualToConstant: NavigationActionView.buttonHeight).priority(.required - 1),
nextButtonShadowContainer.heightAnchor.constraint(greaterThanOrEqualToConstant: NavigationActionView.buttonHeight).priority(.required - 1),
2022-01-05 08:11:35 +01:00
])
2022-01-04 11:30:21 +01:00
backButton.translatesAutoresizingMaskIntoConstraints = false
2022-01-05 08:11:35 +01:00
backButtonShadowContainer.addSubview(backButton)
backButton.pinToParent()
2022-01-05 08:11:35 +01:00
2022-01-04 11:30:21 +01:00
nextButton.translatesAutoresizingMaskIntoConstraints = false
nextButtonShadowContainer.addSubview(nextButton)
nextButton.pinToParent()
// We want the back button to be as small as possible, allowing the next button to take up
// any remaining space. .defaultLow is "the priority level at which a button hugs its
// contents horizontally". Setting this on backButton allows nextButton to eat up remaining
// space. Note that we have to set this on the backButton, not the container, because it's
// backButton's size that determines the compression amount.
backButton.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
// Ensure that the back button has a reasonable minimum tap area.
NSLayoutConstraint.activate([
backButton.widthAnchor.constraint(greaterThanOrEqualToConstant: NavigationActionView.minimumBackButtonWidth).priority(.defaultLow - 1)
])
2022-01-04 11:30:21 +01:00
}
2022-01-05 08:11:35 +01:00
2022-01-04 11:30:21 +01:00
}