iOS 8 UITableView rotation bug

梦想的初衷 提交于 2019-12-06 04:54:16

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

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

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;
    }
}

This appears to be a new problem with iOS8. When you want to rotate an object it no longer appears to rotate around the upper left corner of the object's frame.

Apple docs for iOS8 state that "an object is rotated about it's center point". So when a vertical UITableView is rotated 90 degrees, it may disappear from view because the center point may be off the visible area. In order to make the table appear as if it was rotated about the upper left corner of the table, you must now also translate the frame by an amount equal to the difference between the frame width and frame height.

It's important to note you need to concatenate the transforms in order to get the desired result, like the following:

First create a 90 degree rotation transform:

    CGAffineTransform xform_rotate = CGAffineTransformMakeRotation(-M_PI * 0.5);

Then create a translation amount variable equal to the difference between table width and height:

float translateAmount = (camThumbsTableView.frame.size.height/2)-(camThumbsTableView.frame.size.width/2);

Then concatenate the original rotation transform with the translation:

    CGAffineTransform xform_total = CGAffineTransformTranslate(xform_rotate, translateAmount, translateAmount);

When done, you can now transform your tableView as follows:

    self.camThumbsTableView.transform = xform_total;

This will have the effect of both rotating and translating your tableView such that it now appears to have been rotated around the upper left corner of the tableView instead of about the center point.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!