不等式约束以编程方式快速

时间:2017-08-07 02:52:40

标签: swift constraints

所以我使用代码(没有故事板)为我的应用程序完全编码了整个设置vc,我想知道你如何制作你创建的不等式约束?这意味着如果应用程序在iPhone 7 plus上运行,那么约束就像我将它们编码为一样,但是当你在iPhone上运行应用程序时,一切都在彼此之上而不是正确的比例。我只知道如何使用不等式在故事板中执行此操作,但是如何在代码中实现它?这是我第一次不使用故事板,所以我有点新鲜。

提前谢谢!

约束的一些代码:

func setupProfileImageView() {
    //need x, y, width, height constraints
    profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    profileImageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 120).isActive = true
    profileImageView.widthAnchor.constraint(equalToConstant: 160).isActive = true
    profileImageView.heightAnchor.constraint(equalToConstant: 160).isActive = true

}

2 个答案:

答案 0 :(得分:1)

只需创建两个约束 - 一个用于最小值(greaterThanOrEqualTo),另一个用于最大值(lessThanOrEqualTo)。

myButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 60).isActive = true
myButton.widthAnchor.constraint(lessThanOrEqualToConstant: 120).isActive = true

如果您需要设置优先级,可以采取一些技巧,但似乎并不需要。如果你正确地放置了一切(X和Y),那就应该这样做。

答案 1 :(得分:0)

您最简单的选择是保持您的方法,并根据算法应用常量。例如:

func setupProfileImageView() {
    //need x, y, width, height constraints

    var idealTop : CGFloat = view.frame.height * (some value) //so that it equals a value that changes with each screen size in a way that you feel ok with

    idealTop = idealTop < 60 ? 60 : idealTop //makes sure that the constraint doesnt fall bellow 60..

    profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    profileImageView.topAnchor.constraint(equalTo: view.topAnchor, constant: idealTop).isActive = true
    profileImageView.widthAnchor.constraint(equalToConstant: 160).isActive = true
    profileImageView.heightAnchor.constraint(equalToConstant: 160).isActive = true

}

其次,我建议对于任何意味着元素垂直对齐并且在相同视图层次上的布局,以类似的方式对它们进行距离,并确保每个元素的topAnchor指向它上面的元素是bottomAnchor。您可以做的一个小技巧是将profileImage的topAnchor的值存储到变量中,以便您可以访问其他元素的值,如下所示:

var profileImageTopAnchor : NSLayoutConstraint?

func setupProfileImageView() {
    //need x, y, width, height constraints

    var idealTop : CGFloat = view.frame.height * (some value) //so that it equals a value that changes with each screen size in a way that you feel ok with

    idealTop = idealTop < 60 ? 60 : idealTop //makes sure that the constraint doesnt fall bellow 60..

    profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    profileImageTopAnchor = profileImageView.topAnchor.constraint(equalTo: view.topAnchor, constant: idealTop)
    profileImageTopAnchor.isActive = true //otherwise it wont get activated
    profileImageView.widthAnchor.constraint(equalToConstant: 160).isActive = true
    profileImageView.heightAnchor.constraint(equalToConstant: 160).isActive = true

}
func setupProfileImageView() {

   let topAnchor = profileImageTopAnchor != nil ? profileImageTopAnchor!.constant : (/*Some arbitrary value here .. or repeat as above with a fraction value of the view.frame*/)

    let topAnchorValue : CGFloat = topAnchor * (some value) //this way, the spacing will change based on the spacing you declare on top of your image..

    //And say that you have three other elements stacked vertically..

    elementA.constraint(equalTo: profileImageView.topAnchor, constant: topAnchorValue).isActive = true
    elementB.constraint(equalTo: elementA.topAnchor, constant: topAnchorValue).isActive = true
    elementC.constraint(equalTo: elementB.topAnchor, constant: topAnchorValue).isActive = true
}

您可以进一步检测实际的屏幕大小,并将其与列表相关联以查看正在使用的手机型号..然后应用特定的约束..但随着这句话越来越长,原因也是如此为什么上面的方法更简单!