<bdo id='AtEz4'></bdo><ul id='AtEz4'></ul>
    <i id='AtEz4'><tr id='AtEz4'><dt id='AtEz4'><q id='AtEz4'><span id='AtEz4'><b id='AtEz4'><form id='AtEz4'><ins id='AtEz4'></ins><ul id='AtEz4'></ul><sub id='AtEz4'></sub></form><legend id='AtEz4'></legend><bdo id='AtEz4'><pre id='AtEz4'><center id='AtEz4'></center></pre></bdo></b><th id='AtEz4'></th></span></q></dt></tr></i><div id='AtEz4'><tfoot id='AtEz4'></tfoot><dl id='AtEz4'><fieldset id='AtEz4'></fieldset></dl></div>
      1. <tfoot id='AtEz4'></tfoot>

        <small id='AtEz4'></small><noframes id='AtEz4'>

        <legend id='AtEz4'><style id='AtEz4'><dir id='AtEz4'><q id='AtEz4'></q></dir></style></legend>

        UILabel sizeToFit 和约束

        时间:2023-09-12
        <i id='s7NAY'><tr id='s7NAY'><dt id='s7NAY'><q id='s7NAY'><span id='s7NAY'><b id='s7NAY'><form id='s7NAY'><ins id='s7NAY'></ins><ul id='s7NAY'></ul><sub id='s7NAY'></sub></form><legend id='s7NAY'></legend><bdo id='s7NAY'><pre id='s7NAY'><center id='s7NAY'></center></pre></bdo></b><th id='s7NAY'></th></span></q></dt></tr></i><div id='s7NAY'><tfoot id='s7NAY'></tfoot><dl id='s7NAY'><fieldset id='s7NAY'></fieldset></dl></div>

        1. <small id='s7NAY'></small><noframes id='s7NAY'>

          • <legend id='s7NAY'><style id='s7NAY'><dir id='s7NAY'><q id='s7NAY'></q></dir></style></legend>
            <tfoot id='s7NAY'></tfoot>
              <bdo id='s7NAY'></bdo><ul id='s7NAY'></ul>
                    <tbody id='s7NAY'></tbody>
                • 本文介绍了UILabel sizeToFit 和约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有什么简单的方法可以帮助我使用它们的内容大小动态改变依赖视图的位置?

                  Is there any simple way which can help me to change position of dependent views dynamically using their content size?

                  我想在列中显示几个视图,它们都有不同的内容.我希望它们一个接一个地放置(我使用约束创建了布局,看起来像这样)

                  I want to show several views in column which all have varying content. And I want them to be placed one after another (I've created layout using constraints which looks like this)

                  但每当我更改标签内容并调用 sizeToFit 时,系统似乎会忽略布局.

                  But whenever I change content of labels and call sizeToFit, system seems to ignore layout.

                  目前我只对 height 属性感兴趣,我知道也可以使用约束 rect 并且过去我在 UIView 上编写了许多类别来动态更改大小(我想每个人都这样做了).但也许有一个我不知道的简单方法?

                  At the moment I'm interested only in height property, I know that constraining rect can be used too and in the past I wrote many categories on UIView to change sizes dynamically (I guess everyone did). But maybe there is a simple way which I don't know?

                  推荐答案

                  -sizeToFit 如果您使用自动布局,则不应调用.这是旧"系统的一部分.

                  -sizeToFit should not be called if you are using auto-layout. That's part of the 'old' system.

                  看起来 IB 已经在您的约束中插入了明确的高度(标签旁边的竖线表示这一点).尝试选择标签并点击 Cmd+= 清除这些标签.

                  It looks like IB has inserted explicit heights into your constraints (the vertical bars next to the labels indicate this). Try selecting the labels and hitting Cmd+= to clear these.

                  对于多行标签,您还需要在视图控制器中执行以下操作,以使旋转/调整视图大小时一切正常:

                  For multiline labels you will also need to do the following in your view controller to make everything work correctly when rotating/resizing the view:

                  - (void)updateLabelPreferredMaxLayoutWidthToCurrentWidth:(UILabel *)label
                  {
                      label.preferredMaxLayoutWidth =
                          [label alignmentRectForFrame:label.frame].size.width;
                  }
                  
                  - (void)viewDidLayoutSubviews
                  {
                      [super viewDidLayoutSubviews];
                  
                      [self updateLabelPreferredMaxLayoutWidthToCurrentWidth:self.label1];
                      [self updateLabelPreferredMaxLayoutWidthToCurrentWidth:self.label2];
                      [self updateLabelPreferredMaxLayoutWidthToCurrentWidth:self.label3];
                  
                      [self.view layoutSubviews];
                  }
                  

                  多行标签暴露了自动布局的弱点之一.我们必须更新 preferredMaxLayoutWidth 来强制标签重排并调整其高度,否则如果视图被调整大小/旋转,自动布局不会意识到标签需要重排和调整大小.

                  Multiline labels expose one of the weaknesses of auto-layout. We have to update preferredMaxLayoutWidth to force the label to reflow and adjust its height, otherwise if the view is resized/rotated, auto-layout does not realize the label needs to be reflowed and resized.

                  这篇关于UILabel sizeToFit 和约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:了解自动布局中的乘数以使用相对定位 下一篇:UILabel 有时不能正确换行(自动布局)

                  相关文章

                • <i id='LpYqt'><tr id='LpYqt'><dt id='LpYqt'><q id='LpYqt'><span id='LpYqt'><b id='LpYqt'><form id='LpYqt'><ins id='LpYqt'></ins><ul id='LpYqt'></ul><sub id='LpYqt'></sub></form><legend id='LpYqt'></legend><bdo id='LpYqt'><pre id='LpYqt'><center id='LpYqt'></center></pre></bdo></b><th id='LpYqt'></th></span></q></dt></tr></i><div id='LpYqt'><tfoot id='LpYqt'></tfoot><dl id='LpYqt'><fieldset id='LpYqt'></fieldset></dl></div>

                      <bdo id='LpYqt'></bdo><ul id='LpYqt'></ul>

                      <small id='LpYqt'></small><noframes id='LpYqt'>

                    1. <tfoot id='LpYqt'></tfoot>

                      <legend id='LpYqt'><style id='LpYqt'><dir id='LpYqt'><q id='LpYqt'></q></dir></style></legend>