菜鸟教程小白 发表于 2022-12-12 09:28:26

iOS 9 多行标签在集合可重用标题 View 中具有自动布局功能不起作用


                                            <p><h1>iOS 8.4:</h1>
<p> <a href="/image/pNqC1.png" rel="noreferrer noopener nofollow"><img src="/image/pNqC1.png" alt="Multi-line label works in iOS8.4"/></a> </p>
<h1>在 iOS 9 上完全相同的代码:</h1>
<p> <a href="/image/aK2Od.png" rel="noreferrer noopener nofollow"><img src="/image/aK2Od.png" alt="Multi-line label doesn&#39;t work in iOS9"/></a> </p>
<p>如您所见,在 iOS 8.4 上运行完全相同的代码会产生与 iOS 9 不同的结果。</p>
<p>在 iOS 9 上,第二行<strong>在第一次加载 View 时显示片刻</strong>,然后<strong>缩回</strong>到其单行状态。这个 View 是一个<strong>Header Collection Reusable View</strong>。</p>
<p>我尝试删除标题 View 中的所有其他 View 和不相关的约束,同时只留下有问题的标签(带有 3 个约束),但仍然产生类似的结果,因此它不会由 View 中的其他约束引起。
<a href="/image/gPEa9.png" rel="noreferrer noopener nofollow"><img src="/image/gPEa9.png" alt="All other views removed, still same results"/></a> </p>
<p>想知道是否有其他人遇到这种情况并知道解决方案?</p>
<h1>编辑 1:</h1>
<p>UILabel 的配置:</p>
<p> <a href="/image/LWgaR.png" rel="noreferrer noopener nofollow"><img src="/image/LWgaR.png" alt="Attributes"/></a>
<a href="/image/th2PE.png" rel="noreferrer noopener nofollow"><img src="/image/th2PE.png" alt="Layouts"/></a> </p>
<h1>编辑 2:</h1>
<p>我在这里创建了一个演示项目:<a href="https://github.com/v-ken/TestMultilineUILabeliOS9" rel="noreferrer noopener nofollow">https://github.com/v-ken/TestMultilineUILabeliOS9</a> .我将问题的根源定位为 <code>reloadData</code>。当它被调用时,<code>UILabel</code> 缩回到一行。</p>
<h1>编辑 3:</h1>
<p>上面的演示 GIT 存储库现已更新为修复。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>最终通过在 UIView 中设置 <code>UILabel</code> 的 <code>preferredMaxLayoutWidth</code> 使其适用于 iOS 9。</p>

<pre><code>- (void)layoutSubviews {
    ;
    myLabel.preferredMaxLayoutWidth = myLabel.frame.size.width;
}
</code></pre>

<p>或者,如果您使用的是 UIViewController:</p>

<pre><code>- (void)viewDidLayoutSubviews {
    ;
    myLabel.preferredMaxLayoutWidth = myLabel.frame.size.width;
    ;
}
</code></pre>

<p>引用:</p>

<p> <a href="https://stackoverflow.com/a/20287811/2491738" rel="noreferrer noopener nofollow">https://stackoverflow.com/a/20287811/2491738</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS 9 多行标签在集合可重用标题 View 中具有自动布局功能不起作用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/33212315/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/33212315/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS 9 多行标签在集合可重用标题 View 中具有自动布局功能不起作用