菜鸟教程小白 发表于 2022-12-13 16:11:53

ios - UIStackView - 将图像定位在兄弟的边缘


                                            <p><p>我正在尝试在 iOS 中创建一个包含标签和相关图像的自定义 UITableViewCell。图片需要尽可能靠近标签的后缘。</p>

<p>下面是到目前为止的进度图。红色区域是水平 UIStackView,我在其中放置了 UILabel(绿色)和 UIImageView(青色)。</p>

<p>UILabel 设置为 Lines = 0。</p>

<p>我已经使用了许多 UIStackView 分布和对齐属性,并且过去很好地利用了本文中概述的方法 <a href="https://spin.atomicobject.com/2017/02/20/uistackview-stacking-child-views/" rel="noreferrer noopener nofollow">A UIStackView Hack for Stacking Child Views Compactly</a> .根据那篇文章中的技术,我有第三个透明 View ,它的 ContentHugggingPriority 较低,因此占用了大部分空间。这几乎可以工作,但有些东西导致标签在该固定点处换行,看起来它是总宽度的 1/3。</p>

<p>并非所有行都会显示图像。</p>

<p>有没有人对如何实现这种布局有任何其他建议?我尝试过普通的旧自动布局(没有 UIStackView),但还有其他问题</p>

<p> <a href="/image/b5G3Y.png" rel="noreferrer noopener nofollow"><img src="/image/b5G3Y.png" alt="Current layout"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这可能就是你所追求的......</p>

<p> <a href="/image/S1V4N.png" rel="noreferrer noopener nofollow"><img src="/image/S1V4N.png" alt="enter image description here"/></a> </p>

<p>堆栈 View 被限制为顶部和底部边距、前导边距 <code>+ 12</code> 和尾随边距 <code>>= 12</code>,具有以下设置:</p>

<pre><code>Axis: Horizontal
Alignment: Top
Distribution: Fill
Spacing: 0
</code></pre>

<p> ImageView 的宽度和高度约束为<code>24</code></p>

<p>标签没有约束,但有:</p>

<pre><code>Content Compression Resistance Priority
    Horizontal: Required (1000)
</code></pre>

<p>结果(顶部设置<em>有</em> ImageView ,底部设置<em>没有</em>):</p>

<p> <a href="/image/vCc7g.png" rel="noreferrer noopener nofollow"><img src="/image/vCc7g.png" alt="enter image description here"/></a>
<a href="/image/pAs8R.png" rel="noreferrer noopener nofollow"><img src="/image/pAs8R.png" alt="enter image description here"/></a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UIStackView - 将图像定位在兄弟的边缘,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/56644237/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/56644237/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UIStackView - 将图像定位在兄弟的边缘