似乎有一些帖子與我的問題有關,但似乎沒有任何幫助。 How do I adjust the anchor point of a CALayer, when Auto Layout is being used?iOS 8 CGAffineTransformMakeScale +自動佈局不能正常工作
我有我的應用程序的縮放預覽(作爲教程),標準視圖(和viewcontroller)按比例縮小並顯示在另一個視圖上。
=============
= =
= --------- =
= - - =
= -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,但與6的XCode和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];
}
我想測試你的代碼,並嘗試調查這裏發生了什麼。你可以在github上發佈一個簡單的演示項目嗎? – matt 2014-09-23 13:27:18