简体   繁体   English

UIImageView上的宽度和高度NSLayoutConstraints不起作用

[英]Width and Height NSLayoutConstraints on UIImageView not working

I am setting the .Width and .Top NSLayoutConstraints on a UIImgeView instance inside my View Controller's updateViewConstraints() . 我在视图控制器的updateViewConstraints()的UIImgeView实例上设置了.Width和.Top NSLayoutConstraints。 In the XCode visual debugger i can see the constraints are actually set, but for some reason they are not being used (?) they appear un (content size) and are greyed out. 在XCode可视调试器中,我可以看到约束实际上已设置,但是由于某些原因,约束没有被使用(?),它们显示为un(内容大小),并且显示为灰色。 When i output the constraints using NSLog i can also see they are set, but somehow the image stays the original size ... What does this mean? 当我使用NSLog输出约束时,我也可以看到它们已设置,但是以某种方式图像保持原始大小...这是什么意思?

constraints in parent: [<NSLayoutConstraint:0x7b379590 UIImageView:0x7af897b0.centerX == UIView:0x7b37d2e0.centerX>, 
                        <NSLayoutConstraint:0x7b36e3d0 V:|-(0)-[UIImageView:0x7af897b0]   (Names: '|':UIView:0x7b37d2e0 )>]
constraints in view: [<NSContentSizeLayoutConstraint:0x7ae892b0 H:[UIImageView:0x7af897b0(301)] Hug:251 CompressionResistance:750>, 
                      <NSContentSizeLayoutConstraint:0x7ae834e0 V:[UIImageView:0x7af897b0(365)] Hug:251 CompressionResistance:750>]

在此处输入图片说明

Code that sets the constraints : 设置约束的代码:

/// Image view with the character + ellipse
@IBOutlet weak var charImageView: UIImageView!

override func updateViewConstraints() {
      super.updateViewConstraints()
      charImageView.widthLayoutConstraint?.constant = 301.0
      charImageView.heightLayoutConstraint?.constant = 365.0
}

The extension i use to get widthLayoutConstraint and heightLayoutConstraint 我用来获取widthLayoutConstraintheightLayoutConstraint的扩展名

extension UIView{
  /**
   Gets the width layout constraint defined as the NSLayoutConstraint whose first item is self and first attribute is Height
   */
  var heightLayoutConstraint:NSLayoutConstraint? {
      get{
          for constraint in constraints {
              if  constraint.firstItem as? UIView == self &&
                  constraint.firstAttribute == NSLayoutAttribute.Height
              {
                  return constraint
              }
          }
          return nil
      }
  }

  /**
   Gets the width layout constraint defined as the NSLayoutConstraint whose first item is self and first attribute is Width
   */
  var widthLayoutConstraint:NSLayoutConstraint? {
      get{
          for constraint in constraints {
              if  constraint.firstItem as? UIView == self &&
                  constraint.firstAttribute == NSLayoutAttribute.Width
              {
                  return constraint
              }
          }
          return nil
      }
  }
}

This is how the layout constraints are added initially using IB. 这就是最初使用IB添加布局约束的方式。 As you can see they are added for the Compact Width size Class, which i am using when i see the problem (iPhone4S simulator). 如您所见,它们是为“紧凑宽度”尺寸类添加的,当我看到问题时(iPhone4S模拟器),我正在使用它们。

The constraints are already added via IB and i am updating their constant with code. 约束已经通过IB添加,我正在用代码更新它们的常量。 The debugger shows clearly that the constant IS IN FACT UPDATED but somehow the image gets drawn in the original size. 调试器清楚地表明常数是“实际更新的”,但是以某种方式以原始大小绘制了图像。

在此处输入图片说明 在此处输入图片说明

Grey color constraints show that these constraints are exist but not installed in your views. 灰色约束表明这些约束存在但未安装在视图中。

Refer below image: 请参考下图:

在此处输入图片说明

  1. Select grey constraint 选择灰色约束
  2. Open attribute inspector 打开属性检查器
  3. At bottom you can see Installed option 在底部,您可以看到Installed选项
  4. If it is uncheck then enable this by clicking check mark 如果未选中,则通过单击选中标记来启用它
  5. This will install selected constraint in your view 这将在您的视图中安装选定的约束

Update: 更新:

You can add width constraint using below code: 您可以使用以下代码添加width约束:

self.addConstraint(NSLayoutConstraint(
        item: charImageView,
        attribute: .Width,
        relatedBy: .Equal,
        toItem: nil,
        attribute: .NotAnAttribute,
        multiplier: 1.0,
        constant: 301.0))

You can update constraint value using my other answer: 您可以使用其他答案更新约束值:

Update constraint's constant value programatically 以编程方式更新约束的常数值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM