Существует несколько способов уменьшить количество строк / повторяющихся строк.
Например, используйте массивы с .forEach
для выполнения идентичных задач:
// all subviews need translatesAutoresizingMaskIntoConstraints = false
[scrollview, contentView, descriptionLabel, descriptionTextView, amountLabel, amountTextField, currencyLabel, currencyTextField].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
}
и:
// these subviews all have the same leading, trailing and height constraints
[descriptionLabel, amountLabel, amountTextField, currencyLabel, currencyTextField].forEach {
NSLayoutConstraint.activate([
$0.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: SpentAmountViewOffset.leading),
$0.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: SpentAmountViewOffset.trailing),
$0.heightAnchor.constraint(equalToConstant: SpentAmountViewOffset.height)
])
}
// descriptionTextView has same leading and trailing constraints, but different height
[descriptionTextView].forEach {
NSLayoutConstraint.activate([
$0.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: SpentAmountViewOffset.leading),
$0.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: SpentAmountViewOffset.trailing),
$0.heightAnchor.constraint(equalToConstant: 200.0)
])
}
// top / vertical spacing / bottom constraints for contentView subviews
NSLayoutConstraint.activate([
descriptionLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: SpentAmountViewOffset.top),
descriptionTextView.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: SpentAmountViewOffset.top),
amountLabel.topAnchor.constraint(equalTo: descriptionTextView.bottomAnchor, constant: SpentAmountViewOffset.top),
amountTextField.topAnchor.constraint(equalTo: amountLabel.bottomAnchor, constant: SpentAmountViewOffset.top),
currencyLabel.topAnchor.constraint(equalTo: amountTextField.bottomAnchor, constant: SpentAmountViewOffset.top),
currencyTextField.topAnchor.constraint(equalTo: currencyLabel.bottomAnchor, constant: SpentAmountViewOffset.top),
currencyTextField.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: SpentAmountViewOffset.bottom)
])
Другим вариантом является использование UIStackView
, поэтому вам нужно только применить ограничения на начало, конец, верх и низ к представлению стека:
// add subviews to stack view
[descriptionLabel, descriptionTextView, amountLabel, amountTextField, currencyLabel, currencyTextField].forEach {
stackView.addArrangedSubview($0)
}
// height constraint is the same for these views
[descriptionLabel, amountLabel, amountTextField, currencyLabel, currencyTextField].forEach {
$0.heightAnchor.constraint(equalToConstant: SpentAmountViewOffset.height).isActive = true
}
// different height constraint for descriptionTextView
descriptionTextView.heightAnchor.constraint(equalToConstant: 200.0).isActive = true
// set stackView spacing (vertical space between arranged subviews)
stackView.spacing = SpentAmountViewOffset.top
И, если это все элементы, которые есть в вашем представлении прокрутки, представление стека может заменить «представление содержимого».