“刷新”查看设备旋转

发布于 2024-09-24 11:29:28 字数 427 浏览 1 评论 0原文

我在 viewDidLoad 中设置了视图。所有不同的框架和子视图等都是相对于 self.view 定义的。因此,无论 self.view 的大小如何,子视图总是会缩小或扩展以适应(就像以前一样)。

因此,当我旋转设备时,我希望视图旋转(使用shouldAutoRotateToInterfaceOrientation:...就足够简单了),但子视图保持相同的形状。

调用 [self viewDidLoad]; 使所有元素都适合,但在之前的布局之上放置一个新层(这很明显......但我只是说解释一下我的意思)。

有什么办法可以刷新子视图的框架之类的吗?说实话,我不知道其他人会做什么。我是否必须将所有视图作为属性放入 .h 文件中,并在 didRotate... 上手动执行所有操作?

I have my view set up in viewDidLoad. All the different frames and such of the subviews have been defined relative to self.view. Therefore it doesn't matter what size self.view is the subviews will always shrink or expand to fit (as it were).

So when I rotate my device I want the view to rotate (easy enough with shouldAutoRotateToInterfaceOrientation:...) but the subviews stay the same shape.

Calling [self viewDidLoad]; makes all the elements fit, but puts a new layer on top of the previous layout (which is obvious... but i'm just saying to explain what I mean).

Is there any way to refresh the frames of the subviews or something? I don't know what other people do to be honest. Am I going to have to put ALL of my views into the .h file as properties and do everything manually on didRotate...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜味超标? 2024-10-01 11:29:28

您有三个选择:

  1. 如果自动调整大小蒙版足以定位您的视图,请在创建子视图时为每个子视图分配正确的自动调整大小蒙版。

  2. 如果自动调整大小蒙版不够,请覆盖 willAnimateRotationToInterfaceOrientation:duration: 并在该方法中重新定位子视图。我将创建一个自定义方法,它将方向作为参数,并负责布局所有子视图。然后,您可以从 willAnimateRotationToInterfaceOrientation:duration:viewDidLoad 调用此方法。

  3. 您还可以创建自定义 UIView 子类,并使视图控制器的视图成为此类的实例。然后重写 layoutSubviews 以根据视图的大小定位所有子视图。这种方法意味着您的自定义视图管理其子视图而不是视图控制器。

You have three options:

  1. If autoresizing masks are good enough to position your views, assign the correct autoresizing mask to each subview when you create them.

  2. If autoresizing masks are not sufficient, override willAnimateRotationToInterfaceOrientation:duration: and reposition your subviews in that method. I would create a custom method that takes the orientation as a parameter and is responsible for laying out all subviews. You can then call this method from willAnimateRotationToInterfaceOrientation:duration: and from viewDidLoad.

  3. You could also create a custom UIView subclass and make your view controller's view an instance of this class. Then override layoutSubviews to position all subviews depending on the view's size. This approach implies that your custom view manages its subviews instead of the view controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文