Swift-自动约束ScrollView中的项目

问题描述 投票:0回答:1

我在将我的物品限制在我的UIScrollView时遇到问题,更具体地说trailing - anchors表现很怪异:

enter image description here

如您所见,trailing-anchorsleading-anchors不同。

这些是我的约束:

    scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 130).isActive = true
    scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30).isActive = true
    scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

    emailTextField.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
    emailTextField.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
    emailTextField.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
    emailTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true

    anzeigeNameTextField.topAnchor.constraint(equalTo: emailTextField.topAnchor, constant: 80).isActive = true
    anzeigeNameTextField.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
    anzeigeNameTextField.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
    anzeigeNameTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true

    wishlistHandleTextField.topAnchor.constraint(equalTo: anzeigeNameTextField.topAnchor, constant: 80).isActive = true
    wishlistHandleTextField.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
    wishlistHandleTextField.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
    wishlistHandleTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true

    passwordTextField.topAnchor.constraint(equalTo: wishlistHandleTextField.topAnchor, constant: 80).isActive = true
    passwordTextField.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
    passwordTextField.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
    passwordTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true

    eyeButtonOne.centerYAnchor.constraint(equalTo: passwordTextField.centerYAnchor, constant: 10).isActive = true
    eyeButtonOne.trailingAnchor.constraint(equalTo: passwordTextField.trailingAnchor).isActive = true

    passwordWiederholenTextField.topAnchor.constraint(equalTo: passwordTextField.topAnchor, constant: 80).isActive = true
    passwordWiederholenTextField.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
    passwordWiederholenTextField.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
    passwordWiederholenTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true

    eyeButtonTwo.centerYAnchor.constraint(equalTo: passwordWiederholenTextField.centerYAnchor, constant: 10).isActive = true
    eyeButtonTwo.trailingAnchor.constraint(equalTo: passwordWiederholenTextField.trailingAnchor).isActive = true

    documentsLabel.topAnchor.constraint(equalTo: passwordWiederholenTextField.topAnchor, constant: 80).isActive = true
    documentsLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    documentsLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    documentsLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true

    signUpButton.topAnchor.constraint(equalTo: documentsLabel.topAnchor, constant: 80).isActive = true
    signUpButton.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
    signUpButton.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
    signUpButton.heightAnchor.constraint(equalToConstant: 50).isActive = true

我做错了什么?我必须在UIScrollView内约束不同吗?如果是这样,如何以及为什么?

ios swift uiscrollview constraints
1个回答
0
投票
为您创建示例代码。希望对您有用
© www.soinside.com 2019 - 2024. All rights reserved.