ios - 如何根据内容制作居中可调整大小的 View

标签 ios xcode constraints

我想在其中创建一个 View ,其中有 3 个具有动态高度的标签。
我希望出现在文本中心的标签很小,当文本变大时会变大最后一个标签的优先级(描述)。

我得到了什么

enter image description here

预期结果

1- 小说明

enter image description here

2-大描述

enter image description here

此外,如果描述很长,则标签的高度不应超过图像的边界,顶部和底部约束应

对此问题的任何修复

最佳答案

您可以使用 UIStackView满足您的需求。

只需按照下面列出的步骤即可获得此结果:

  • 添加 UIImageView和配置约束:前导、顶部、底部和宽度(常数或比例)。
  • 添加 UILabels (并且不要忘记将行数设置为 0)
    enter image description here
  • 选择标签并将它们添加到堆栈 View 。设置 Fill Alignment 中的值和 Distribution字段。
    enter image description here
  • 添加 leadingtrailing堆栈 View 的约束
    enter image description here
  • 向 Stack View 添加垂直居中约束
    enter image description here

  • 在原始问题的编辑后更新 - “如果描述很长,标签的高度不应超过图像的边界,顶部和底部约束应该”
  • 选择堆栈 View 和 ImageView 并添加顶部和底部边缘约束。
    enter image description here
  • 选择刚刚添加的顶部边缘约束并更改 RelationAttributes InspectorGreater Than or Equal .在此之后选择底部边缘约束并更改 RelationLess Than or Equal .
  • 选择前两个标签并设置在Size Inspector垂直 Content Compression Resistance Priority到 751。
    enter image description here

    仅此而已

    enter image description here
    enter image description here
  • 关于ios - 如何根据内容制作居中可调整大小的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58267458/

    相关文章:

    ios - 使用 Swift 钥匙串(keychain)包装器时发生意外崩溃

    ios - 具有动态高度的 UITableView 和 UITableViewCell,只有 WillDisplay 才知道

    ios - 从开发机到 iOS 设备的数据文件

    ios - 将 Assets 视频发送到网络服务 - iOS

    objective-c - Objective-C : Anonymous Blocks, 为什么以及何时?

    Enum 的 Java 通用绑定(bind)(约束)

    c# - 接口(interface)的对象,但对实现接口(interface)的类型有约束

    ios - 如何将整个 XCode 项目切换到之前的提交

    iphone - NSTimer 没有停止?

    swift - 直接对泛型类型进行类型约束与使用 'where' 子句之间是否存在实际差异?