gpt4 book ai didi

iOS:setFrame 不再适用于 viewDidLoad 或 viewWillAppear

转载 作者:技术小花猫 更新时间:2023-10-29 11:12:36 24 4
gpt4 key购买 nike

我更喜欢禁用 autoresizesSubviews 并使用 setFrame 来放置我所有的 subview 。从 iOS 6 开始,事情似乎发生了很大变化。当我在 viewDidLoad 中的 View 上调用 setFrame 时,没有任何效果。我从 viewWillAppear 尝试过;同样的事情。

setFrame 调用将在 willAnimateRotationToInterfaceOrientation 中工作,但最初不会像在 iOS 5 中那样调用它。

有人可以澄清一下我应该从哪里布局我的 View 吗?

最佳答案

我猜你想从 UIViewController 布局你的 View 。您是否尝试过在 viewDidLayoutSubviews 中执行您的布局任务? :

- (void)viewDidLayoutSubviews

根据 Apple 的文档:

Your view controller can override this method to make changes after the view lays out its subviews.

由于您的 View 本身不对其 subview 进行布局(您不使用自动调整大小、自动布局或 layoutSubViews:),因此您可以在此方法中完成布局任务。

不过,优雅的方法是使用自定义父 UIView 并在那里执行所有布局,覆盖 UIView 的 layoutSubViews:(除非您动态添加/删除 View )。引自 Apple 关于 "How View Controllers Participate in the View Layout Process" 的文档:

Ideally, the views themselves perform all of the necessary work to reposition themselves, without requiring the view controller to participate in the process at all. However, if the view controller adds and removes views dynamically, a static layout in Interface Builder may not be possible. In this case, the view controller is a good place to control the process, because often the views themselves only have a limited picture of the other views in the scene.

关于iOS:setFrame 不再适用于 viewDidLoad 或 viewWillAppear,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12645248/

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