gpt4 book ai didi

ios - [super layoutSubviews] 的正确调用方式是什么?

转载 作者:技术小花猫 更新时间:2023-10-29 10:48:04 44 4
gpt4 key购买 nike

我刚刚在适用于 iOS 的 Facebook SDK 中看到他们在 layoutSubviews 方法的末尾而不是开头调用了 [super layoutSubviews];

据我所知,我们应该始终将其作为第一行。以不同的方式实现它会导致任何意外的 UI 行为吗?

- (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];
}

最佳答案

根据UIView Class Reference ,

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 版本构建的应用程序的工件。

对于较新版本的 iOS,您应该调用 [super layoutSubviews]在您实现的开始。否则,父类(super class)将在您执行自定义布局后重新排列您的 subview ,有效地忽略您对 layoutSubviews() 的实现。 .

关于ios - [super layoutSubviews] 的正确调用方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31347772/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com