iOS 8 UITableView轮换错误 [英] iOS 8 UITableView rotation bug

查看:87
本文介绍了iOS 8 UITableView轮换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在XCode 6上编译应用程序后,我注意到只有在iOS 8上运行时才会出现一个奇怪的错误:
UITableView 接错了内部尺寸。

So after compiling an app on XCode 6, I noticed a strange bug that happens only when running on iOS 8: The UITableView takes the wrong inner dimensions after updating its frame.

现在我将尝试解释确切情况:
我们有 UITableView 侧旋,基本上是水平 UITableView 。它通过 tableView.transform = CGAffineTransformMakeRotation(-M_PI / 2); 发生。
现在设置转换后,然后设置它的框架 - 一切都很好。
当然,系统在大多数情况下会向父节点发送另一个帧更改,因为它需要将父节点设置为实际大小而不是XIB大小或任何初始化大小。在那一刻 - 当我重新发布子视图,包括表格视图 - 一切都出错了。
实际上,表视图的框架只是设置为包含视图的边界,但随后是内部滚动视图(在iOS 8中 UITableView 里面有另一个 UIScrollView ,名为 UITableViewWrapperView 。如 UITableView 本身就是一个 UIScrollView ,我无法弄清楚为什么他们需要另一个......)取一个高度等于父宽度。并且高度实际上是宽度属性,仅旋转。

Now I'll try to explain the exact situation: We have a UITableView rotated on its side, which basically makes a horizontal UITableView. It happens through tableView.transform = CGAffineTransformMakeRotation(-M_PI / 2);. Now after setting the transform, and then settings its frame - everything is fine. But of course the system in most cases sends the parent another frame change because it needs to set the parent to the real sizes and not the XIB sizes or any initialization size. In that moment - when I relayout the subviews, including the table view - everything goes wrong. Actually the frame of the table view is simply set to the bounds of the containing view, but then the inner scrollview (In iOS 8 the UITableView has another UIScrollView inside it, called UITableViewWrapperView. As UITableView is a UIScrollView by itself, I can't figure out why they needed another one...) takes a "height" which equals the parent width. And "height" is actually the width property, only rotated.

现在我们可以很容易地估计他们有将内部 UIScrollView 的宽度与父 UITableView 的实际宽度相关联的错误,这可能是通过阅读 .frame.size.width 而不是 .bounds.size.width
但奇怪的是,在调查 UITableView 子视图的框架时 - 似乎它们都很好!所以它必须是某处的渲染问题。

Now we can easily estimate the they have a bug with relating the width of the inner UIScrollView to the actual width of the parent UITableView, which could possibly be by reading the .frame.size.width instead of the .bounds.size.width. But the strange thing is that when investigating the frame of the subviews of the UITableView- it seems that they are all good! So it must be a rendering problem somewhere.

所以我们留下了一个水平表,顶部有一个空白,因为单元格的高度是320而不是568,而单元格的宽度很好,设置为320.

So we are left with a horizontal table which has a blank gap on top, because the "height" of the cells is 320 instead of 568, while the "width" of the cells is fine, set to 320.

我会很高兴听到其他人遇到这个问题(或者来自Apple),但我终于找到了一个解决方案,并将问题发布在此处,供我和其他人参考。

I'll be very happy to hear from other people experiencing this problem (Or from Apple), but I have finally found a solution and posting it here with the question, for future reference for me and for others.

推荐答案

因此,使其表现的变化不是这样做的:

So the change that made it behave, was instead of doing this:

- (void)layoutSubviews
{
    tableView.frame = self.bounds;
}

我已经重置了转换,将帧设置为UITableView的边界在变换后期望局部,然后设置变换并设置正确的帧。这有点令人困惑,但在这里:

I have reset the transform, set the frame to the bounds which the UITableView would expect locally after the transform, and then set the transform and set the correct frame. This is a bit confusing, but here it goes:

- (void)layoutSubviews
{
    if (UIDevice.currentDevice.systemVersion.floatValue >= 8.f)
    {
        // iOS 8 layout bug! Table's "height" taken from "width" after changing frame. But then if we cancel transform, set width/height to the final width/height, and rotate it and set to the virtual width/height - it works!

        CGRect rotatedFrame = self.bounds,
        unrotatedFrame = rotatedFrame;
        unrotatedFrame.size.width = rotatedFrame.size.height;
        unrotatedFrame.size.height = rotatedFrame.size.width;

        tableView.transform = CGAffineTransformIdentity;
        tableView.frame = unrotatedFrame;
        tableView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
        tableView.frame = rotatedFrame;
    }
    else
    {
        tableView.frame = self.bounds;
    }
}

这篇关于iOS 8 UITableView轮换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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