【问题标题】:UIImageView length is redundant when using autolayout使用自动布局时 UIImageView 长度是多余的
【发布时间】:2023-03-31 01:11:01
【问题描述】:

我正在尝试向我的 UITableViewCell 展示一个具有可选长度的 ImageView。这是我的布局:

但我的结果看起来像这样(顶部和底部的冗余): 如何解决?

【问题讨论】:

  • 首先,lmao @ 示例图片。其次,确保您对顶部的“Title Title”文本视图和底部的按钮都有垂直空间约束,常量 == 0
  • @AO 当然可以。我设置的。
  • 您是否尝试进入视图调试器以确认额外的空间确实是 UIImageView 的额外高度?如果你强制将高度设为一个特定的常数,你还能看到那个间距吗?
  • 我使用 UITableViewAutomaticDimension 作为 UITableViewDelegate 中的行高。这不是一个常数
  • 正确,我试图推荐调试的想法

标签: ios objective-c uitableview uiimageview autolayout


【解决方案1】:

问题在于ContentHuggingPriorityContentCompressionResistancePriority

尝试使用这个

[NSLayoutConstraint autoSetPriority:ALLayoutPriorityRequired forConstraints:^{
    [yourImageView autoSetContentCompressionResistancePriorityForAxis:ALAxisHorizontal];
    [yourImageView autoSetContentHuggingPriorityForAxis:ALAxisHorizontal];
}];

【讨论】:

    【解决方案2】:

    在您的代码中没有任何东西可以检测图像的大小。

    1. 在图像视图上设置高度限制。常量无关紧要,因为您稍后会以编程方式更改它。

    2. 将该高度约束设为 IBOutlet 并将其连接到 IB

      @property (strong, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeight
      
    3. 获取图像的大小。 Reference

      UIImage * img = [UIImage imageNamed:@"someImage.png"];
      CGSize imgSize = img.size;
      
    4. 更改 NSLayoutConstraint 常量。

      self.imageViewHeight.constant = imgSize.height
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多