ios - 为什么自动布局忽略了一些约束?

标签 ios objective-c autolayout

该应用程序有一个简单的 TableView Controller ,可以在左侧显示图像。 (见第一张图片)。 UIView与 x 轴对齐,具有前导空间约束,并且具有最大宽度。我本以为这也足以防止图像失真超过 130 像素,但事实并非如此。当设备处于横向模式时,问题更加明显。

为什么忽略某些约束(请参阅 View 调试器屏幕截图中的某些约束如何变灰),更重要的是,如何才能使具有固定高度的可变宽度图像在单元格中不失真?

Autolayout all blue and not complaining

some of the constraints are being greyed out

in landscape mode the problem is more clearly visible.

最佳答案

您问了两个问题,我将分别回答:

  • 为什么有些约束被忽略:

  • They aren't being ignored actually. You have not set any constraints for those specific areas. When you apply a constraint, a respective line is highlighted in blue. When you haven't applied a constraint for a specific side, it remains grey.


  • 如何使具有固定高度的可变宽度图像不会在单元格中出现扭曲?

  • You can use either AspectFit or AspectFill, so that the aspect ratio of your images is not disturbed and they don't looks stretched.

    You should also remove the width constraint from the ImageView. Then you can use AspectFill if you know that your height will not increase from 130. If your image height can be more than 130, you should rather use AspectFit.

    关于ios - 为什么自动布局忽略了一些约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30998828/

    相关文章:

    javascript - 是否有相当于 ios 自动布局约束概念的 JavaScript 或 Web 开发库?

    ios - 为什么NSMutableSet可以添加的对象多于容量

    ios - AVMutableVideoCompositionInstruction 在 AVMutableVideoComposition 中被忽略

    objective-c - Objective-C 中基于堆栈的数组声明?

    objective-c - 使用 NSOperationQueue 或 GCD 进行异步解析

    swift - 使用 UITextView 在 UIView 中以编程方式添加约束

    ios - 不支持的编译器 'com.apple.compilers.llvmgcc42' xcode 5 DP

    ios - 找不到签名证书 "iOS Development"

    ios - 调整文本大小以适应 Swift 中的标签

    ios - 键盘隐藏时 UITableView 略微上升