NSLayoutConstraint在不同设备上的问题

时间:2016-03-04 23:29:54

标签: swift nslayoutconstraint

我使用的是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 plus,则视图位于底部边缘上方30点,但如果我使用iphone 6,则视图会消失。

1 个答案:

答案 0 :(得分:1)

问题是你已经对高度和宽度进行了硬编码。不要将常量用于宽度和高度,而是将它们定义为superview的其他视图的倍数。

  

如何在不进行硬编码的情况下设置宽度和高度?你有一个例子吗?假设我想看到屏幕一半的1/3,即屏幕的1/6

使用乘数来完成此任务。如果您只有一个子视图,则可以将宽度和高度定义为superview的倍数:

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
])

产量:

enter image description here

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

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)
])

enter image description here

显然,如果您想要实例化NSLayoutConstraint,然后执行addConstraint,就像您在原始示例中所做的那样,那么请您感到自由。上面的语法(在iOS 9中引入)只是一个更简洁的再现,但概念与上面相同,即应该适当地固定边缘,但只将宽度/高度定义为另一个视图的倍数,而不是使用一些硬编码常数。