【问题标题】:Getting the correct frame inside willRotateToInterfaceOrientation在 willRotateToInterfaceOrientation 中获取正确的框架
【发布时间】:2011-11-14 16:26:20
【问题描述】:

我的子视图在纵向和横向时有不同的布局。 布局基于 superview 的框架,而不是微不足道的(无法使用自动调整大小的弹簧和支柱来实现)。

问题是,如果我在 willRotateToInterfaceOrientation 期间更改 subViews 的框架,动画是正确的,但框架是 superView 仍然是旧方向的框架。

如果我在didRotateFromInterfaceOrientation期间更改子视图,帧是superView确实是正确的,但帧更改仅发生在旋转动画之后,看起来不太好。

改变方向时改变子视图框架的正确方法是什么?

【问题讨论】:

  • 我通过在每个子视图中编写一个处理帧更改部分的方法来解决这个问题。我将在 willRotateToInterfaceOrientation 中设置父视图的框架,然后使用 CGFrameMake 函数生成一个新的类似框架并将其传递给每个子视图。它对我有用....检查它是否有帮助。
  • 我不太明白你的回答。在 willRotateToInterface 我仍然不知道 superView 的框架,因为旋转还没有发生。这就是问题所在。
  • 我不使用 superView 的框架来旋转子视图。我计算旋转后superView帧的值,然后根据计算创建一个新的CGRect,然后将其传递给每个子视图的handleOrientation方法。如果您无法计算旋转后 superView 的帧的值是多少,那么上面的方法将不适合您。
  • 是的,很遗憾,我无法计算旋转后 superView 的框架。
  • 您能否提供有关您查看的内容类型的线索?没有那个我很难提出任何建议。

标签: iphone layout rotation orientation autoresize


【解决方案1】:

我认为您可以尝试覆盖 layoutSubviews。在那里,您可以根据当前方向布局子视图,而无需担心 will/did RotateFromInterfaceOrientation。每次布置视图时都会调用layoutSubviews。这将帮助您确定帧随动画变化的时间。

【讨论】:

  • 但这意味着我将所有布局逻辑移动到 View 而不是 ViewController。目前我正在根据 UIViewController 的属性设置 subView 的布局。将逻辑移动到 UIView 意味着我必须继承 UIView 并将所有参数移动到这个自定义子类。我不完全确定这个逻辑应该在 UIView 而不是 UIVIewController 中,但我会尝试。
【解决方案2】:

此功能可满足您的需求,因为视图框架已从旋转发生变化,并且不会停止自定义动画。

-(void)willAnimateRotationToInterfaceOrientation (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    float width = UIInterfaceOrientationIsPortrait(toInterfaceOrientation) ? [[UIScreen mainScreen] applicationFrame].size.width : [[UIScreen mainScreen] applicationFrame].size.height;
//do something with the width now
//or you can get the height the same way
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    相关资源
    最近更新 更多