0

我将 PureLayout 用于我的编程视图约束。我以为我正确设置了它们,尺寸,X 和 Y 已解决。但是,当我在 SE 和 7 Plus 上运行代码时,视图无法正确对齐。

这是代码片段:

 if (shouldSetupConstraints) {
        // bg
        backgroundView.autoPinEdgesToSuperviewEdges()
        // positions
        emailTextField.autoPinEdge(toSuperviewEdge: .top, withInset: 170)
        emailTextField.autoPinEdge(toSuperviewEdge: .left, withInset: 60)
        passwordTextField.autoPinEdge(toSuperviewEdge: .top, withInset: 250)
        passwordTextField.autoPinEdge(toSuperviewEdge: .left, withInset: 60)
        loginButton.autoPinEdge(toSuperviewEdge: .top, withInset: 350)
        loginButton.autoPinEdge(toSuperviewEdge: .left, withInset: 75)
        recoverButton.autoPinEdge(toSuperviewEdge: .top, withInset: 410)
        recoverButton.autoPinEdge(toSuperviewEdge: .left, withInset: 60)
        // confirms setup is done
        shouldSetupConstraints = false
    }

我认为视网膜屏幕上的屏幕点是相同的,那么为什么这些数字会有差异呢?

4

2 回答 2

0

您只是为超级视图边缘提供约束。您应该相对于彼此使用约束。我不确定 %100,但这也应该解决屏幕问题。

这是你应该怎么做的:

if (shouldSetupConstraints) {
    // bg
    backgroundView.autoPinEdgesToSuperviewEdges()
    // positions

    emailTextField.autoPinEdge(toSuperviewEdge: .top, withInset: 170)
    emailTextField.autoPinEdge(toSuperviewEdge: .left, withInset: 60)

    passwordTextField.autoPinEdge(.top, to: .bottom, of:  emailTextField, withOffset: 80.0, relation: .equal)
    passwordTextField.autoAlignAxis(.vertical, toSameAxisOf:  emailTextField)

    loginButton.autoPinEdge(.top, to: .bottom, of:  passwordTextField, withOffset: 100.0, relation: .equal)
    loginButton.autoPinEdge(toSuperviewEdge: .left, withInset: 75)

    recoverButton.autoPinEdge(.top, to: .bottom, of:  loginButton, withOffset: 60.0, relation: .equal)
    recoverButton.autoAlignAxis(.vertical, toSameAxisOf:  emailTextField)

    // confirms setup is done
    shouldSetupConstraints = false
}
于 2018-06-04T11:04:57.863 回答
0

iPhone SE (320x568) 和 7 Plus (414x736)的逻辑分辨率不同

不要从顶部或左侧定位每个约束,而是尝试使用给定超级视图的每一侧。

于 2017-09-01T12:20:03.517 回答