0

I cant seem to get my contentView to scale properly thus I cant click the continue button when the app is running on smaller screens. I tried to create a UIScrollView using method from this answer: https://stackoverflow.com/a/27227174

func drawUIElements() {
    view.addSubview(scrollView)
    scrollView.addSubview(contentView)


    scrollView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    scrollView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

    contentView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
    contentView.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor).isActive = true
    contentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
    contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
    contentView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
    contentView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true

    contentView.addSubview(bigQuestionMarkIcon)
    contentView.addSubview(bigGuessText)
    contentView.addSubview(smallInfoTextLabel)
    contentView.addSubview(fieldStackView)
    fieldStackView.addSubview(userNameTextField)
    fieldStackView.addSubview(emailTextField)
    fieldStackView.addSubview(passwordTextField)
    fieldStackView.addSubview(repeatPasswordTextField)
    fieldStackView.addSubview(continueButton)
    fieldStackView.addSubview(smallInfoBtn)

    fieldStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 12.0).isActive = true
    fieldStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -12.0).isActive = true
    fieldStackView.topAnchor.constraint(equalTo: smallInfoTextLabel.bottomAnchor, constant: 10.0).isActive = true
    fieldStackView.heightAnchor.constraint(equalToConstant: 250.0).isActive = true

    bigQuestionMarkIcon.centerXAnchor.constraint(equalTo: contentView.centerXAnchor, constant: 0).isActive = true
    bigQuestionMarkIcon.topAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.topAnchor, constant: 5.0).isActive = true

    bigGuessText.centerXAnchor.constraint(equalTo: contentView.centerXAnchor, constant: 0.0).isActive = true
    bigGuessText.topAnchor.constraint(equalTo: bigQuestionMarkIcon.bottomAnchor, constant: -10.0).isActive = true

    smallInfoTextLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor, constant: 0.0).isActive = true
    smallInfoTextLabel.topAnchor.constraint(equalTo: bigGuessText.bottomAnchor, constant: 3.0).isActive = true

    userNameTextField.topAnchor.constraint(equalTo: fieldStackView.topAnchor, constant: 0.0).isActive = true
    userNameTextField.leftAnchor.constraint(equalTo: fieldStackView.leftAnchor, constant: 0.0).isActive = true
    userNameTextField.rightAnchor.constraint(equalTo: fieldStackView.rightAnchor, constant: 0.0).isActive = true

    emailTextField.topAnchor.constraint(equalTo: userNameTextField.bottomAnchor, constant: 10.0).isActive = true
    emailTextField.leftAnchor.constraint(equalTo: fieldStackView.leftAnchor, constant: 0.0).isActive = true
    emailTextField.rightAnchor.constraint(equalTo: fieldStackView.rightAnchor, constant: 0.0).isActive = true

    passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: 10.0).isActive = true
    passwordTextField.leftAnchor.constraint(equalTo: fieldStackView.leftAnchor, constant: 0.0).isActive = true
    passwordTextField.rightAnchor.constraint(equalTo: fieldStackView.rightAnchor, constant: 0.0).isActive = true

    repeatPasswordTextField.topAnchor.constraint(equalTo: passwordTextField.bottomAnchor, constant: 10.0).isActive = true
    repeatPasswordTextField.leftAnchor.constraint(equalTo: fieldStackView.leftAnchor, constant: 0.0).isActive = true
    repeatPasswordTextField.rightAnchor.constraint(equalTo: fieldStackView.rightAnchor, constant: 0.0).isActive = true

    smallInfoBtn.centerXAnchor.constraint(equalTo: fieldStackView.centerXAnchor, constant: 0.0).isActive = true
    smallInfoBtn.topAnchor.constraint(equalTo: continueButton.bottomAnchor, constant: 5.0).isActive = true

    continueButton.leftAnchor.constraint(equalTo: fieldStackView.leftAnchor, constant: 12.0).isActive = true
    continueButton.rightAnchor.constraint(equalTo: fieldStackView.rightAnchor, constant: -12.0).isActive = true
    continueButton.heightAnchor.constraint(equalTo: fieldStackView.widthAnchor, multiplier: 11.0/75.0).isActive = true
    continueButton.topAnchor.constraint(equalTo: repeatPasswordTextField.bottomAnchor, constant: 15.0).isActive = true
}

UI Debug Image

pkamb
  • 33,281
  • 23
  • 160
  • 191
articapps
  • 19
  • 10

2 Answers2

0

Check this thread https://stackoverflow.com/a/60586641/4587775

BTW, is better to use leading and trailing anchors, since left and right are absolute values to the screen or the control. If you use different locales in the future LTR you might need to change all of them manually.

I would recommend the use of NSLayoutConstraint.activate([]) your code will be much cleaner, and it might be also a good idea use some static helper in your app for the app for the Spacers, or in the same class but then you don't deal with numbers for margins but with constants.

0

you need to give scrollview bottom to that last button bottom in your view.

scrollView.bottomAnchor.constraint(equalTo: continueButton.bottomAnchor, constant: 15.0).isActive = true

OR

scrollView.bottomAnchor.constraint(equalTo: continueButton.bottomAnchor, constant: -15.0).isActive = true