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