当iOS8上的下CGAffineTransform旋转的UIView不会调整 [英] UIView not resizing when rotated with a CGAffineTransform under iOS8

查看:157
本文介绍了当iOS8上的下CGAffineTransform旋转的UIView不会调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIViewController只旋转设备时,旋转的一些它的子视图。这iOS7下工作正常,但iOS8上休息下。似乎了UIView的边界由iOS8上下的变换调整。这是出乎意料的。

I have a UIViewController that only rotates some of it subviews when the device is rotated. This works fine under iOS7 but breaks under iOS8. It appears that the UIView's bounds are adjusted by the transform under iOS8. This was unexpected.

下面是一些code:

@interface VVViewController ()
@property (weak, nonatomic) IBOutlet UIView *pinnedControls;
@property (nonatomic, strong) NSMutableArray *pinnedViews;

@end

@implementation VVViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.pinnedViews = [NSMutableArray array];
    [self.pinnedViews addObject:self.pinnedControls];
}

-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    [UIViewController rotatePinnedViews:self.pinnedViews forOrientation:self.interfaceOrientation];
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];

    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation) && UIInterfaceOrientationIsLandscape(self.interfaceOrientation))  {
        [UIViewController rotatePinnedViews:self.pinnedViews forOrientation:toInterfaceOrientation];
    }
}

@end

我们已经取得了在UIViewController的一个类别来处理这种行为。下面是相关的code:

We've made a category on UIViewController to handle this behavior. Here's the pertinent code:

@implementation UIViewController (VVSupport)

+ (void)rotatePinnedViews:(NSArray *)views forOrientation:(UIInterfaceOrientation)orientation {
    const CGAffineTransform t1 = [UIViewController pinnedViewTansformForOrientation:orientation counter:YES];
    const CGAffineTransform t2 = [UIViewController pinnedViewTansformForOrientation:orientation counter:NO];
    [views enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
        // Rotate the view controller
        view.transform = t1;
        [view.subviews enumerateObjectsUsingBlock:^(UIView *counterView, NSUInteger idx, BOOL *stop) {
            // Counter-rotate the controlsUIin the view controller
            counterView.transform = t2;
        }];
    }];
}

+ (CGAffineTransform)pinnedViewTansformForOrientation:(UIInterfaceOrientation)orientation counter:(BOOL)counter {
    CGAffineTransform t;
    switch ( orientation ) {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            t = CGAffineTransformIdentity;
            break;

        case UIInterfaceOrientationLandscapeLeft:
            t = CGAffineTransformMakeRotation(counter ? M_PI_2 : -M_PI_2);
            break;

        case UIInterfaceOrientationLandscapeRight:
            t = CGAffineTransformMakeRotation(counter ? -M_PI_2 : M_PI_2);
            break;
    }

    return t;
}

@end

下面是笔尖的样子:

命名寄托在笔尖的UIView是pinnedControls的IBOutlet中:

The UIView named pinned in the nib is the IBOutlet of pinnedControls:

当我在iOS7或iOS8上在纵向模式下运行此我得到这样的:

When I run this in portrait mode under iOS7 or iOS8 I get this:

和我看到iOS7下所期望的结果在横向模式:

And I see the desired outcome under iOS7 in landscape mode:

但在iOS8上(GM)我不明白这种行为。这是我所看到的,而不是:

But under iOS8 (GM) I do not get this behavior. This is what I see instead:

注意的UILabel的文本已固定标签的中心从固定的UIView,这并没有改变大小以容纳旋转的底部保持其距离。这UIView的有其所有边缘固定到顶部,左侧,超级视图底部和右侧。

Notice that the center of the UILabel with the text "Pinned Label" is maintaining its distance from the bottom of the pinned UIView, which has not changed size to accommodate the rotation. That UIView has all its edges pinned to the top, left, bottom and right sides of the super view.

在我看来,该变换财产自动布局在不同的交互iOS8上。我有点困惑在这里。我知道我不能依赖在框架上。我可能只是开始手动设置界限,但只是似乎是错误的做法,基本上是围绕做自动布局结束运行。

It looks to me that the transform property interacts with Auto Layout differently under iOS8. I'm a bit baffled here. I know I can't rely on the frame. I may just start manually setting bounds but that just seems like the wrong thing to do, essentially do an end run around Auto Layout.

推荐答案

这更是一个比工作一个修复周围所以它可能不会帮助在类似情况下大家。我的问题是,外固定的观点正在调整再次变换将应用于后。

This is more of a work around than a fix so it may not help everybody in similar situations. My problem was that the outer "pinned" view was being resized again after the transform was applied.

我的解决办法是改变对固定视图的约束是中心垂直,水平居中,而宽度和高度等于一个常数。

My solution was to change the constraints on the pinned view to be center vertically, center horizontally, and width and height equal a constant.

然后,在viewDidLoad中,我设置了高度的和宽度固定视图的框架的是主屏幕的高度。这使得视图广场,所以我不关心,如果它得到一个额外的旋转。

Then, in viewDidLoad, I set the height and width of the pinned view's frame to be the height of the main screen. This makes the view square so I don't care if it gets an extra rotate.

这篇关于当iOS8上的下CGAffineTransform旋转的UIView不会调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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