iOS 8 CGAffineTransformMakeScale + autolayout not working any more

冷暖自知 提交于 2020-01-04 06:27:50

问题


There seems to be a some post related to my problem but none seems to help. How do I adjust the anchor point of a CALayer, when Auto Layout is being used?

I have a scaled preview of my app (as a tutorial), which the standard view (and viewcontroller) scaled down and shown on another view.

=============
=           =
= --------- =
= -       - =
= -Preview- =
= -       - =
= --------- =
=    View   =
=============

For this I use this code:

previewViewController.view.transform = CGAffineTransformMakeScale(scale*2, scale*2);

I also need this scaled view to align with the tutorial view controller. For this I created a "placeholder view" which I want my scaled view to match in size.

For this I use:

-(void)setConstraintsForPreviewViewController
{
    NSArray *attributes = @[@(NSLayoutAttributeLeftMargin), @(NSLayoutAttributeRightMargin), @(NSLayoutAttributeTopMargin), @(NSLayoutAttributeBottomMargin)];
    [attributes enumerateObjectsUsingBlock:^(NSNumber *obj, NSUInteger idx, BOOL *stop) {
        NSLayoutAttribute attribute = [obj integerValue];
        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewControllerPlaceholderView
                                                              attribute:attribute
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:previewViewController.view
                                                              attribute:attribute
                                                             multiplier:1
                                                               constant:0]];
    }];
}

This worked good i iOS 7, but building with XCode 6 and iOS 8 this seems to break. It seems as it now first sets the constraints (adjust to placeholder) and then scales it down (resulting in a too small preview).

Code:

-(void)setupPreviewViewController
{
    float scale = [self scale];
    previewViewController = [self.storyboard instantiateViewControllerWithIdentifier:[PLACEHOLDER_VIEWCONTROLLER_NAMES objectAtIndex:self.identifier]];

    previewViewController.view.transform = CGAffineTransformMakeScale(scale*2, scale*2);

    [self.view addSubview:previewViewController.view];
    previewViewController.view.translatesAutoresizingMaskIntoConstraints = NO;

    [self addChildViewController:previewViewController];

    [self setConstraintsForPreviewViewController];

    self.viewControllerPlaceholderToImageViewSpaceConstraint.constant = ([self hasNavigationBar] ? (44 * scale * 2) : -22 * scale * 2);

    self.viewControllerPlaceholderToBottomSpaceConstraint.constant = IS_WIDESCREEN ? 160 : 131;

    previewViewController.view.userInteractionEnabled = NO;

    [self.view setNeedsLayout];
}

回答1:


It seems like an iOS 8 bug to me. I had the same issue. My solution was to disable autolayout for preview and resort to autoresizingMask.

preview.translatesAutoresizingMaskIntoConstraints = YES;
preview.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
preview.frame = view.bounds;

But my preview view is a plain UIImageView. I have no clue how it would work with some complex autolayout-based preview view.

I have pretty similar setup as yours, I do use placeholder VC on top too, but it's built using autoresizingMask too, seemed like easier to center things vertically.

If you use any constraints inside of preview view and experience issues, then I would try to wrap preview in one more UIView and see if it works. [view (autoresizingMask, custom frame, scaled)] -> [view (autoresizingMask = Flexible W+H)] -> [Preview (Autolayout)]



来源:https://stackoverflow.com/questions/25976491/ios-8-cgaffinetransformmakescale-autolayout-not-working-any-more

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