菜鸟教程小白 发表于 2022-12-12 14:49:45

ios - 如果 sizeToFit 依赖于 layoutSubviews,如何实现?


                                            <p><p>在 <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/layoutSubviews" rel="noreferrer noopener nofollow">layoutSubviews</a> 的文档中,苹果说:</p>

<blockquote>
<p>You should not call this method directly.</p>
</blockquote>

<p>我正在尝试实现 <code>sizeToFit</code>。我希望它在所有 subview 上放置一个紧密的边界框。在确定这样的边界框之前,我必须布局 subview 。这意味着我必须调用 Apple 不赞成的 <code>layoutSubviews</code>。如何在不违反 Apple 规则的情况下解决这个难题?</p>

<pre><code>- (void)layoutSubviews
{
;

self.view0.frame = something;
self.view1.frame = somethingElse;
}

- (void)sizeToFit
{
   ;

   self.frame = CGRectMake(
   self.frame.origin.x,
   self.frame.origin.y,
   MAX(
       self.view0.frame.origin.x + self.view0.frame.size.width,
       self.view1.frame.origin.x + self.view1.frame.size.width
   ),
   MAX(
       self.view0.frame.origin.y + self.view0.frame.size.height,
       self.view1.frame.origin.y + self.view1.frame.size.height
   )
);
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>不应覆盖 <code>-sizeToFit</code>。而是用 View 的当前边界大小覆盖 <code>-sizeThatFits:</code>,它由 <code>-sizeToFit</code> 内部调用。</p>

<blockquote>
<p>You should not override this method. If you want to change the default sizing information for your view, override the sizeThatFits: instead. That method performs any needed calculations and returns them to this method, which then makes the change. – <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/sizeToFit" rel="noreferrer noopener nofollow">UIView Class Reference</a></p>
</blockquote>

<hr/>

<p>同样,即使您将覆盖 <code>-sizeToFit</code>,也很可能没有理由立即执行布局。您只需调整 View 的大小,即设置其边界大小。这会触发对<code>-setNeedsLayout</code> 的调用,将 View 标记为<em>需要布局</em>。但除非您想为 View 设置动画,否则不必立即应用新布局。</p>

<p>这种延迟更新模式的要点是,如果您执行多次连续更新,它会节省大量时间,因为实际更新只执行一次。</p>

<hr/>

<p>我通常这样做。它就像一个魅力。</p>

<pre><code>#pragma mark - Layout &amp; Sizing

- (void)layoutSubviews
{
    ;
}

- (CGSize)sizeThatFits:(CGSize)size
{
    CGFloat const width = size.width;
    CGFloat const height = ;

    return CGSizeMake(width, height);
}

- (CGFloat)calculateHeightForWidth:(CGFloat)width applyLayout:(BOOL)apply
{
    CGRect const topViewFrame = ({
      CGRect frame = CGRectZero;
      ...
      frame;
    });

    CGRect const bottomViewFrame = ({
      CGRect frame = CGRectZero;
      ...
      frame;
    });

    if (apply) {
      self.topView.frame = topViewFrame;
      self.bottomView.frame = bottomViewFrame;
    }

    return CGRectGetMaxY(bottomViewFrame);
}
</code></pre>

<p>请注意,示例代码适用于可以以任何宽度显示的 View ,并且容器会要求特定宽度的首选高度。</p>

<p>不过,您可以轻松地调整其他布局样式的代码。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如果 sizeToFit 依赖于 layoutSubviews,如何实现?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/29287431/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/29287431/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如果 sizeToFit 依赖于 layoutSubviews,如何实现?