似乎有一些帖子与我的问题相关,但似乎没有任何帮助。使用自动布局时如何调整 CALayer 的锚点? https://stackoverflow.com/questions/12943107/how-do-i-adjust-the-anchor-point-of-a-calayer-when-auto-layout-is-being-used
我有我的应用程序的缩放预览(作为教程),标准视图(和视图控制器)缩小并显示在另一个视图上。
=============
= =
= --------- =
= - - =
= -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 构建时似乎会出现问题。看起来它现在首先设置约束(调整占位符),然后缩小它(导致预览太小)。
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];
}