不同设备上的 NSLayoutConstraint 问题

NSLayoutConstraint Issue on different devices

我正在使用 NSLayoutconstraint,但是当从 iphone 6 plus 传递到 iphone 6.

时,它似乎弄乱了视图的位置

它应该可以工作,因为我正在创建与视图属性相关的约束,在使用不同设备的情况下应该更改该属性。

这里有一些约束示例:

v=getBitmapView("V -.-")
    self.view.addSubview(v)
    var myConstraint =
        NSLayoutConstraint(item: v,
            attribute: NSLayoutAttribute.CenterX,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self.view,
            attribute: NSLayoutAttribute.CenterX,
            multiplier: 1.0,
            constant: 0)



    self.view.addConstraint(myConstraint)
     myConstraint =
    NSLayoutConstraint(item: v,
        attribute: NSLayoutAttribute.BottomMargin,
        relatedBy: NSLayoutRelation.Equal,
        toItem: self.view,
        attribute: NSLayoutAttribute.BottomMargin,
        multiplier: 1.0,
        constant: -30)



    self.view.addConstraint(myConstraint)
    myConstraint =
        NSLayoutConstraint(item: v,
            attribute: NSLayoutAttribute.Width,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self.view,
            attribute: NSLayoutAttribute.Width,
            multiplier: 1.0,
            constant: -200)



    self.view.addConstraint(myConstraint)
    myConstraint =
        NSLayoutConstraint(item: v,
            attribute: NSLayoutAttribute.Height,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self.view,
            attribute: NSLayoutAttribute.Height,
            multiplier: 1.0,
            constant: -650)



    self.view.addConstraint(myConstraint)

如果我 运行 iphone 6 加上视图位于底部边距上方 30 点,但如果我使用 iphone 6,视图就会消失。

问题是您对高度和宽度进行了硬编码。与其使用宽度和高度常量,不如将它们定义为父视图的某个其他视图的倍数。

How can I set the width and height without hard-coding? Do you have an example? Let's say i want to have a view 1/3 of the half of the screen that is 1/6 of the screen

使用乘数来完成这个。如果你只有一个子视图,你可以将宽度和高度定义为父视图的倍数:

NSLayoutConstraint.activateConstraints([
    blueView.topAnchor.constraintEqualToAnchor(view.topAnchor),                              // pin to top left corner
    blueView.leftAnchor.constraintEqualToAnchor(view.leftAnchor),

    blueView.widthAnchor.constraintEqualToAnchor(view.widthAnchor, multiplier: 1.0 / 3.0),   // one third the width
    blueView.heightAnchor.constraintEqualToAnchor(view.heightAnchor, multiplier: 1.0 / 2.0)  // one half the height
])

结果:

或者,有时,你定义了一堆视图来跨越整个父视图,然后定义它们相对于彼此的宽度,例如,上半部分是三分之一蓝色和三分之二红色,下半部分是全绿:

NSLayoutConstraint.activateConstraints([
    blueView.topAnchor.constraintEqualToAnchor(view.topAnchor),             // pin blue and red to the top
    redView.topAnchor.constraintEqualToAnchor(view.topAnchor),

    blueView.leftAnchor.constraintEqualToAnchor(view.leftAnchor),           // pin blue to left edge
    redView.leftAnchor.constraintEqualToAnchor(blueView.rightAnchor),       // pin red adjacent to blue view
    redView.rightAnchor.constraintEqualToAnchor(view.rightAnchor),          // pin red to right edge
    redView.widthAnchor.constraintEqualToAnchor(blueView.widthAnchor, multiplier: 2.0), // but make red twice as wide as blue (i.e. 2/3rds entire view)

    greenView.topAnchor.constraintEqualToAnchor(blueView.bottomAnchor),     // define green to be below blue
    greenView.topAnchor.constraintEqualToAnchor(redView.bottomAnchor),      //   and red views

    greenView.heightAnchor.constraintEqualToAnchor(blueView.heightAnchor),  // but make it same height as blue

    greenView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor),      // and pin it to the bottom of superview

    greenView.leftAnchor.constraintEqualToAnchor(view.leftAnchor),          // and green spans entire superview
    greenView.rightAnchor.constraintEqualToAnchor(view.rightAnchor)
])

显然,如果您更愿意实例化一个 NSLayoutConstraint 然后执行 addConstraint,就像您在原始示例中所做的那样,那么请放心。上面的语法(在iOS 9中介绍)只是一种更简洁的表述,但概念与上面相同,即应该根据需要固定边缘,但只定义width/height为另一种观点,不使用一些硬编码常量。