Ответ Шуртугала частично верен - вам нужно bottomAnchor
на thirdTextField
, чтобы дать contentView
некоторую высоту:
thirdTextField.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true
См. Комментарии к этому ответу для дополнительного обсуждения.
Однако я добавляю этот ответ в качестве подсказки. Возможно, вам будет гораздо проще работать с ограничениями и автоматическим макетом, если логически сгруппировать ограничения, как показано в отредактированном коде ниже:
class ViewController: UIViewController {
lazy var myScrollView: UIScrollView = {
let view = UIScrollView(frame: .zero)
view.backgroundColor = .lightGray
return view
}()
lazy var contentView: UIView = {
let view = UIView()
view.backgroundColor = .blue
return view
}()
var firstTextField = UITextField()
var secondtextField = UITextField()
var thirdTextField = UITextField()
var fourthTextField = UITextField()
var firstLabel = UILabel()
var secondLabel = UILabel()
var aButton = UIButton()
var keyboardToolBar = UIToolbar()
override func viewDidLoad() {
super.viewDidLoad()
// Add first label
view.addSubview(firstLabel)
firstLabel.backgroundColor = .yellow
firstLabel.text = "Yellow Label"
// Add first textfield
view.addSubview(firstTextField)
firstTextField.backgroundColor = .orange
firstTextField.placeholder = "Orange TextField"
firstTextField.inputAccessoryView = keyboardToolBar
// Add scrollView
view.addSubview(myScrollView)
// Add ContentView inside scrollview
myScrollView.addSubview(contentView)
// Add secondTextfield - inside contentview
contentView.addSubview(secondtextField)
secondtextField.backgroundColor = .red
secondtextField.placeholder = "Red TextField"
secondtextField.inputAccessoryView = keyboardToolBar
// Add thirdtextfield - inside contentview
contentView.addSubview(thirdTextField)
thirdTextField.backgroundColor = .brown
thirdTextField.placeholder = "Brown TextField"
thirdTextField.inputAccessoryView = keyboardToolBar
// elements outside of scrollview
// add fourthtextField
view.addSubview(fourthTextField)
fourthTextField.backgroundColor = .systemTeal
fourthTextField.placeholder = "Teal Textfield"
fourthTextField.inputAccessoryView = keyboardToolBar
// Add second label
view.addSubview(secondLabel)
secondLabel.backgroundColor = .green
secondLabel.text = "Green Label"
// Add button
view.addSubview(aButton)
aButton.backgroundColor = .systemBlue
aButton.setTitle("A Button", for: .normal)
[firstLabel, firstTextField, myScrollView, contentView, secondtextField, thirdTextField, fourthTextField, secondLabel, aButton].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
}
NSLayoutConstraint.activate([
// firstLabel 100-pts from top, 20-pts on each side
firstLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
firstLabel.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20),
firstLabel.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20),
// firstTextField 150-pts from top, 20-pts on each side
firstTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: 150),
firstTextField.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20),
firstTextField.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20),
// myScrollView 20-pts from bottom of firstTextField
// 20-pts on each side
// 200-pts from bottom of view
myScrollView.topAnchor.constraint(equalTo: firstTextField.bottomAnchor, constant: 20),
myScrollView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20.0),
myScrollView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20.0),
myScrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -200.0),
// contentView (subview of myScrollView) 20-pts on each side
// this will automatically define the scrollView's .contentSize
// however, it does NOT control the SIZE of contentView
contentView.topAnchor.constraint(equalTo: myScrollView.topAnchor, constant: 20.0),
contentView.leftAnchor.constraint(equalTo: myScrollView.leftAnchor, constant: 20.0),
contentView.rightAnchor.constraint(equalTo: myScrollView.rightAnchor, constant: -20.0),
contentView.bottomAnchor.constraint(equalTo: myScrollView.bottomAnchor, constant: -20.0),
// secondtextField (subview of contenView) 100-pts from top, 20-pts on each side
secondtextField.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 100),
secondtextField.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
secondtextField.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
// secondtextField (subview of contenView) 500-pts from top of secondtextField (should probably be from bottom of secondTextField)
// 20-pts on each side
// (end up being placed 500 below secondtextfield to use through vertical scrolling)
thirdTextField.topAnchor.constraint(equalTo: secondtextField.topAnchor, constant: 500),
thirdTextField.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
thirdTextField.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
// constraints which define the SIZE of contentView...
// if we want contentView to fit the width of myScrollView (with 20-pts "padding" on each side)
contentView.widthAnchor.constraint(equalTo: myScrollView.widthAnchor, constant: -40),
// thirdTextField bottom 20-pts from contentView bottom (this will define contentView's height)
thirdTextField.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -20),
// fourthTextField 300-pts from bottom of firstTextField, 10-pts on each side
fourthTextField.topAnchor.constraint(equalTo: firstTextField.bottomAnchor, constant: 300),
fourthTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
fourthTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
// secondLabel 150-pts from bottom of view, 20-pts on each side
secondLabel.topAnchor.constraint(equalTo: view.bottomAnchor, constant: -150),
secondLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
secondLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
// aButton 100-pts from bottom of view, 20-pts on each side
aButton.topAnchor.constraint(equalTo: view.bottomAnchor, constant: -100),
aButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
aButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
])
// Add Target to BUtton
aButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
}
@objc func buttonTapped() -> Void {
print("button tapped")
}
}
Результат:
после прокрутки: