God*_*her 5 ios swift uistackview
我有一个带有UIImageView (1:1 Aspect Rati, Height 32) 和label的水平堆栈视图。由于它有一个高度,stackView 会在右边切断我的标签,不让我有不止一行。
我希望 Horizontal StackView 的高度增长,因为标签需要显示自己的文本或使用 UIImageView 高度(如果它更大)。
这是完整的解释,
如果你没有给 Imageview 提供任何高度,那么它的样子
ImageView 增长
如果你给 Imageview 赋予高度,
标签缩小到 imageview 的高度
将 ImageView 放入如下视图中,
现在,为图像视图提供适当的约束。现在它可以正常工作。
输出:
GitHub Repo:源代码 检查 FirstViewController 内的代码
解释:
步骤1: 更改StackView的约束,使height >= minValue值。之后它的高度将根据 UILabel 的高度自动调整。
步骤2: 设置文本后,调用UILabel的sizeToFit方法。这将改变 UILabel 的框架。
lable.text = "SomeText Here. It will expalnd the StackView.omeText Here. It will expalnd the StackView.omeText Here. It will expalnd the StackView.omeText Here. It will expalnd the StackView.omeText Here. It will expalnd the StackView.omeText Here. It will expalnd the StackView.omeText Here. It will expalnd the StackView.omeText Here. It will expalnd the StackView."
lable.sizeToFit()
Run Code Online (Sandbox Code Playgroud)
步骤3: 将图像的缩放模式设置为Aspect Fit,并设置其高度和宽度约束。
第四步: 根据需求设置Stackview的对齐方式,我将其设置为顶部,使图像和标签保持在顶部。
最后结果:
限制条件:
| 归档时间: |
|
| 查看次数: |
4890 次 |
| 最近记录: |