调用[super layoutSubviews]的正确方法是什么? [英] What is the correct way to call [super layoutSubviews]?

查看:217
本文介绍了调用[super layoutSubviews]的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在iOS版Facebook SDK中看到,他们在layoutSubviews方法的结尾而不是在layoutSubviews方法的开头调用[super layoutSubviews];.

I just saw in the Facebook SDK for iOS that they call [super layoutSubviews]; at the end and not at the beginning of the layoutSubviews method.

据我所知,我们应该始终作为第一行. 可以通过其他方式实现它导致任何意外的UI行为吗?

As far as I know, we should always do it as the first line. Can implementing it a different way cause any unexpected UI behavior?

- (void)layoutSubviews
{
  CGSize size = self.bounds.size;
  CGSize longTitleSize = [self sizeThatFits:size title:[self _longLogInTitle]];
  NSString *title = (longTitleSize.width <= size.width ?
                     [self _longLogInTitle] :
                     [self _shortLogInTitle]);
  if (![title isEqualToString:[self titleForState:UIControlStateNormal]]) {
    [self setTitle:title forState:UIControlStateNormal];
  }

  [super layoutSubviews];
}

推荐答案

根据

此方法的默认实现在iOS 5.1及更早版本上不执行任何操作.否则,默认实现将使用您设置的任何约束条件来确定任何子视图的大小和位置.

The default implementation of this method does nothing on iOS 5.1 and earlier. Otherwise, the default implementation uses any constraints you have set to determine the size and position of any subviews.

因此,Facebook SDK示例应用在实施结束时调用[super layoutSubviews]可能是该应用最初为iOS 5.1之前的iOS版本构建的产物.

Thus, that the Facebook SDK example app calls [super layoutSubviews] at the end of their implementation could be an artifact of the app being initially built for an iOS version prior to iOS 5.1.

对于最新版本的iOS,您应在实现开始时调用[super layoutSubviews].否则,超类将在您执行自定义布局后重新排列您的子视图,从而有效地忽略了layoutSubviews()的实现.

For more recent versions of iOS, you should call [super layoutSubviews] at the beginning of your implementation. Otherwise, the superclass will rearrange your subviews after you do the custom layout, effectively ignoring your implementation of layoutSubviews().

这篇关于调用[super layoutSubviews]的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆