似乎有一些与我的问题相关的帖子,但似乎没有任何帮助。使用自动布局时,如何调整 CALayer 的锚点?
我有我的应用程序的缩放预览(作为教程),标准视图(和视图控制器)按比例缩小并显示在另一个视图上。
=============
= =
= --------- =
= - - =
= -Preview- =
= - - =
= --------- =
= View =
=============
为此,我使用以下代码:
previewViewController.view.transform = CGAffineTransformMakeScale(scale*2, scale*2);
我还需要这个缩放视图与教程视图控制器对齐。为此,我创建了一个“占位符视图”,我希望我的缩放视图在大小上匹配。
为此,我使用:
-(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]];
}];
}
这在 iOS 7 中运行良好,但使用 XCode 6 和 iOS 8 构建这似乎中断了。似乎它现在首先设置约束(调整为占位符),然后将其缩小(导致预览太小)。
代码:
-(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];
}