gpt4 book ai didi

ios - 可以在 viewDidLoad 中使用 layoutIfNeeded 来确定角半径大小吗?

转载 作者:行者123 更新时间:2023-12-01 17:22:18 24 4
gpt4 key购买 nike

在 Xcode 8 iOS 项目的 viewDidLoad 中调用 layoutIfNeeded 是否安全,如下面的代码?

- (void)viewDidLoad {
[super viewDidLoad];

[self.view layoutIfNeeded];

CGFloat frameHeight = CGRectGetHeight(self.frame);
subview.layer.cornerRadius = frameHeight * 0.5;
}

它在没有 layoutIfNeeded 的 Xcode 7 中运行良好。

最佳答案

如果有问题,可以在 viewDidLayoutSubviews() 中调用它。

来自 Apple iOS 10 的发行说明是:

Sending layoutIfNeeded to a view is not expected to move the view, but in earlier releases, if the view had translatesAutoresizingMaskIntoConstraints set to NO, and if it was being positioned by constraints, layoutIfNeeded would move the view to match the layout engine before sending layout to the subtree. These changes correct this behavior, and the receiver’s position and usually its size won’t be affected by layoutIfNeeded.

Some existing code may be relying on this incorrect behavior that is now corrected. There is no behavior change for binaries linked before iOS 10, but when building on iOS 10 you may need to correct some situations by sending -layoutIfNeeded to a superview of the translatesAutoresizingMaskIntoConstraints view that was the previous receiver, or else positioning and sizing it before (or after, depending on your desired behavior) layoutIfNeeded.

Third party apps with custom UIView subclasses using Auto Layout that override layoutSubviews and dirty layout on self before calling super are at risk of triggering a layout feedback loop when they rebuild on iOS 10. When they are correctly sent subsequent layoutSubviews calls they must be sure to stop dirtying layout on self at some point (note that this call was skipped in release prior to iOS 10)."

基本上你不能在 View 的子对象上调用 layoutIfNeeded - 现在调用 layoutIfNeeded 必须在 superView 上,你仍然可以在 viewDidLayoutSubviews 中调用它。

关于ios - 可以在 viewDidLoad 中使用 layoutIfNeeded 来确定角半径大小吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39730408/

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