viewWillTransitionToSize 和错误的 navigationBar 和 statusBarFrame 高度 [英] viewWillTransitionToSize and wrong navigationBar and statusBarFrame heights

查看:129
本文介绍了viewWillTransitionToSize 和错误的 navigationBar 和 statusBarFrame 高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取 GLKViewController 子类的可见绘图区域.在 iOS 7 中,我会确定方向,然后相应地调整 [UIScreen mainScreen].bounds.size.然后我会减去 navigationBar 和 statusBarFrame 的高度,瞧!- 我的绘图区域的实际大小.

I am trying to get the visible drawing area for a subclass of GLKViewController. In iOS 7, I would determine the orientation and then adjust [UIScreen mainScreen].bounds.size accordingly. Then I would subtract the navigationBar and statusBarFrame heights and voila! - the actual size of my drawing area.

现在我正在尝试使用 viewWillTransitionToSize 更新 iOS 8,但是当我旋转设备时(我打开了 UIInterfaceOrientationMaskAll),传递的大小是错误的.实际上,视图的宽度是正确的,而不是高度(涉及导航栏和状态栏的高度).

Now I'm trying to update for iOS 8 with viewWillTransitionToSize, however when I rotate the device (I have UIInterfaceOrientationMaskAll on), the size that is passed is wrong. In fact, the width is correct for the view, but not the height (which involves the navbar and statusbar heights).

我在 SO 上看到了几个与此相关的问题,但没有真正解决这个问题,至少没有帮助我.

I've seen a couple of questions on SO that relate but nothing that actually deals with this issue, at least nothing that helped me.

对我来说这似乎没有什么有趣的事情,如果我错了,请纠正我:

This doesn't seem to me to be doing anything interesting, correct me if I'm wrong:

- (void)viewWillTransitionToSize : (CGSize) size
       withTransitionCoordinator : (id<UIViewControllerTransitionCoordinator>) coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    NSLog(@"size changed : orig(%f,%f) mainScreen(%f,%f)", size.width, size.height, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

    [self updateViews:size];
}

过渡到横向的输出:

size changed : orig(568.000000,256.000000) mainScreen(320.000000,568.000000)

从 viewWillTransitionToSize 传递的高度似乎是:

where the height passed from viewWillTransitionToSize appears to be:

320 - (2*32) = 256

然后下一个旋转到纵向:

and then the next rotation to portrait:

size changed : orig(320.000000,536.000000) mainScreen(568.000000,320.000000)

从 viewWillTransitionToSize 传递的高度似乎是:

where the height passed from viewWillTransitionToSize appears to be:

568 - 32 = 536

32 恰好是横向模式下导航栏的高度(如果这意味着什么).

32 happens to be the height of the navbar in landscape mode (if that means anything).

所以我的问题是:viewWillTransitionToSize 如何获得这个大小?它如何考虑根据方向改变大小或消失的额外条形?最重要的是,为什么它使用了这些不正确的高度?

So my questions are: how does viewWillTransitionToSize get this size? How does it take into account the extra bars that change sizes or disappear depending on the orientation? And most importantly, why is it using these incorrect heights?

推荐答案

查看视图帧大小 viewWillTransitionToSize:withTransitionCoordinationar: 时,您可能会在更新之前获得帧.

When looking at view frame sizes viewWillTransitionToSize:withTransitionCoordinationar:, you will likely get the frames before the update.

最好在这个方法中使用animateAlongsideTransition:completion::

Better use animateAlongsideTransition:completion: inside this method:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size
          withTransitionCoordinator:coordinator];


    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
    {
          //update views here, e.g. calculate your view
    }
                                 completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {
     }];
}

这篇关于viewWillTransitionToSize 和错误的 navigationBar 和 statusBarFrame 高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆