Я бы лично использовал здесь представления стека, это определенно даст вам больше гибкости.
Вот окончательный результат:
![enter image description here](https://i.stack.imgur.com/iVbuG.png)
И реализация будет такой:
import UIKit
class ViewController: UIViewController {
private let flightBoardView: FlightBoardView = {
$0.translatesAutoresizingMaskIntoConstraints = false
return $0
}(FlightBoardView())
override func viewDidLoad() {
super.viewDidLoad()
setup()
view.backgroundColor = .white
}
private func setup() {
setupViews()
setupConstraints()
}
private func setupViews() {
flightBoardView.set(fromCountry: "Germany", fromCity: "Frankfurt", toCountry: "France", toCity: "Paris")
view.addSubview(flightBoardView)
}
private func setupConstraints() {
flightBoardView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
flightBoardView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
}
И ваш FlightBoardView
может быть подклассом UIView
(в случае, если вы хотите повторно использовать его где-нибудь позже в своем коде и избежать дублирования) и посмотрите вот так:
class FlightBoardView: UIView {
private lazy var departureStackView: UIStackView = {
$0.distribution = .fill
$0.alignment = .leading
$0.axis = .vertical
$0.spacing = 5
$0.translatesAutoresizingMaskIntoConstraints = false
return $0
}(UIStackView(arrangedSubviews: [fromCountryLabel, fromCityLabel]))
private lazy var destinationStackView: UIStackView = {
$0.distribution = .fill
$0.alignment = .leading
$0.axis = .vertical
$0.spacing = 5
$0.translatesAutoresizingMaskIntoConstraints = false
return $0
}(UIStackView(arrangedSubviews: [toCountryLabel, toCityLabel]))
private lazy var containerStackView: UIStackView = {
$0.distribution = .fill
$0.alignment = .center
$0.axis = .horizontal
$0.spacing = 20
$0.translatesAutoresizingMaskIntoConstraints = false
return $0
}(UIStackView(arrangedSubviews: [departureStackView, planeImageView, destinationStackView]))
private let fromCountryLabel: UILabel = {
$0.textColor = .black
$0.font = UIFont.systemFont(ofSize: 30)
$0.translatesAutoresizingMaskIntoConstraints = false
return $0
}(UILabel())
private let fromCityLabel: UILabel = {
$0.textColor = .lightGray
$0.font = UIFont.systemFont(ofSize: 15)
$0.translatesAutoresizingMaskIntoConstraints = false
return $0
}(UILabel())
private let toCountryLabel: UILabel = {
$0.textColor = .black
$0.font = UIFont.systemFont(ofSize: 30)
$0.translatesAutoresizingMaskIntoConstraints = false
return $0
}(UILabel())
private let toCityLabel: UILabel = {
$0.textColor = .lightGray
$0.font = UIFont.systemFont(ofSize: 15)
$0.translatesAutoresizingMaskIntoConstraints = false
return $0
}(UILabel())
private let planeImageView: UIImageView = {
$0.contentMode = .scaleAspectFit
$0.image = UIImage(named: "airplane")?.withRenderingMode(.alwaysTemplate)
$0.tintColor = .systemBlue
$0.translatesAutoresizingMaskIntoConstraints = false
return $0
}(UIImageView())
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder: NSCoder) {
fatalError()
}
private func setup() {
addSubview(containerStackView)
NSLayoutConstraint.activate([
containerStackView.topAnchor.constraint(equalTo: topAnchor),
containerStackView.leftAnchor.constraint(equalTo: leftAnchor),
containerStackView.bottomAnchor.constraint(equalTo: bottomAnchor),
containerStackView.rightAnchor.constraint(equalTo: rightAnchor),
planeImageView.widthAnchor.constraint(equalToConstant: 50),
planeImageView.heightAnchor.constraint(equalTo: planeImageView.widthAnchor),
])
}
func set(fromCountry: String, fromCity: String, toCountry: String, toCity: String) {
fromCountryLabel.text = fromCountry
fromCityLabel.text = fromCity
toCountryLabel.text = toCountry
toCityLabel.text = toCity
}
}
Конечно, вы можете сделать то же самое с раскадровками и файлами xib, если хотите, но, по крайней мере, это дает вам рабочий пример, и вы можете настроить его в соответствии со своими потребностями.
Я также добавил метод set(fromCountry:fromCity:toCountry:toCity:)
, чтобы вы могли легко изменять данные из родительского представления.