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

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

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

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

      1. iOS Autolayout:调整大小父视图中的 UILabels 问题

        时间:2023-09-11

              <bdo id='Kxoul'></bdo><ul id='Kxoul'></ul>
                <tbody id='Kxoul'></tbody>
              <tfoot id='Kxoul'></tfoot>
            • <legend id='Kxoul'><style id='Kxoul'><dir id='Kxoul'><q id='Kxoul'></q></dir></style></legend>

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

                • <i id='Kxoul'><tr id='Kxoul'><dt id='Kxoul'><q id='Kxoul'><span id='Kxoul'><b id='Kxoul'><form id='Kxoul'><ins id='Kxoul'></ins><ul id='Kxoul'></ul><sub id='Kxoul'></sub></form><legend id='Kxoul'></legend><bdo id='Kxoul'><pre id='Kxoul'><center id='Kxoul'></center></pre></bdo></b><th id='Kxoul'></th></span></q></dt></tr></i><div id='Kxoul'><tfoot id='Kxoul'></tfoot><dl id='Kxoul'><fieldset id='Kxoul'></fieldset></dl></div>
                  本文介绍了iOS Autolayout:调整大小父视图中的 UILabels 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个只包含 UILabel 的视图.此标签包含多行文本.父级具有可变宽度,可以使用平移手势调整大小.我的问题是,当我调整 UILabel 的大小时,它不会重新计算其高度以使所有内容仍然可见,它只是将其切断.

                  I've got a view that contains only a UILabel. This label contains multiline text. The parent has a variable width that can be resized with a pan gesture. My problem is that when I do this resizing the UILabel does not recalculate its height such that all of the content is still visible, it simply cuts it off.

                  我已经设法通过一些 hack 来修复它,但运行起来非常慢:

                  I've managed to fix it with a bit of a hack but it is horribly slow to run:

                  - (void)layoutSubviews {
                  
                      CGSize labelSize = [self.labelDescription sizeThatFits:CGSizeMake(self.frame.size.width, FLT_MAX)];
                  
                      if (self.constraintHeight) {
                          [self removeConstraint:self.constraintHeight];
                      }
                  
                      self.constraintHeight = [NSLayoutConstraint constraintWithItem:self.labelDescription attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:labelSize.height];
                  
                      [self addConstraint:self.constraintHeight];
                  
                      [super layoutSubviews];
                  }
                  

                  这不应该通过自动布局自动发生吗?

                  Shouldn't this happen automatically with autolayout?

                  编辑

                  我的视图结构是:

                  UIScrollView
                  ---> UIView
                       ---> UILabel
                  

                  以下是 UIScrollView 的约束:

                  Here are the constraints on the UIScrollView:

                  <NSLayoutConstraint:0x120c4860 H:|-(>=32)-[DescriptionView:0x85c81c0]   (Names: '|':UIScrollView:0x85db650 )>,
                  <NSLayoutConstraint:0x120c48a0 H:|-(32@900)-[DescriptionView:0x85c81c0] priority:900   (Names: '|':UIScrollView:0x85db650 )>,
                  <NSLayoutConstraint:0x120c48e0 H:[DescriptionView:0x85c81c0(<=576)]>,
                  <NSLayoutConstraint:0x120c4920 H:[DescriptionView:0x85c81c0]-(>=32)-|   (Names: '|':UIScrollView:0x85db650 )>,
                  <NSLayoutConstraint:0x120c4960 H:[DescriptionView:0x85c81c0]-(32@900)-| priority:900   (Names: '|':UIScrollView:0x85db650 )>,
                  <NSLayoutConstraint:0x8301450 DescriptionView:0x85c81c0.centerX == UIScrollView:0x85db650.centerX>,
                  

                  以下是对 UIView 的约束:

                  Here are the constraints on the UIView:

                  <NSLayoutConstraint:0x85c4580 V:|-(0)-[UILabel:0x85bc7b0]   (Names: '|':DescriptionView:0x85c81c0 )>,
                  <NSLayoutConstraint:0x85c45c0 H:|-(0)-[UILabel:0x85bc7b0]   (Names: '|':DescriptionView:0x85c81c0 )>,
                  <NSLayoutConstraint:0x85c9f80 UILabel:0x85bc7b0.trailing == DescriptionView:0x85c81c0.trailing>,
                  <NSLayoutConstraint:0x85c9fc0 UILabel:0x85bc7b0.centerY == DescriptionView:0x85c81c0.centerY>
                  

                  UILabel 本身对它没有任何限制,除了将其固定到其父对象的边缘

                  The UILabel itself has no constraints on it, aside from pinning it to the edges of its parent

                  推荐答案

                  在向 Apple 提出错误后,我已修复此问题.多行文本需要两次布局的问题,这一切都依赖于属性 preferredMaxLayoutWidth

                  I've fixed this issue after raising a bug with Apple. The issue that multiline text requires a two-pass approach to layout and it all relies on the property preferredMaxLayoutWidth

                  以下是需要添加到包含多行标签的视图控制器的相关代码:

                  Here is the relevant code that needs to be added to a view controller that contains a multiline label:

                  - (void)viewWillLayoutSubviews
                  {
                      // Clear the preferred max layout width in case the text of the label is a single line taking less width than what would be taken from the constraints of the left and right edges to the label's superview
                      [self.label setPreferredMaxLayoutWidth:0.];
                  }
                  
                  - (void)viewDidLayoutSubviews
                  {
                      // Now that you know what the constraints gave you for the label's width, use that for the preferredMaxLayoutWidth—so you get the correct height for the layout
                      [self.label setPreferredMaxLayoutWidth:[self.label bounds].size.width];
                  
                      // And then layout again with the label's correct height.
                      [self.view layoutSubviews];
                  }
                  

                  这篇关于iOS Autolayout:调整大小父视图中的 UILabels 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:正确使用intrinsicContentSize和sizeThatFits:在带有自动布局的UIView子类上 下一篇:iOS Autolayout 垂直相等的空间来填充父视图

                  相关文章

                    • <bdo id='mCdR0'></bdo><ul id='mCdR0'></ul>

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

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

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