2014-09-22 72 views
2

似乎有一些帖子與我的問題有關,但似乎沒有任何幫助。 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]; 
} 
+0

我想測試你的代碼,並嘗試調查這裏發生了什麼。你可以在github上發佈一個簡單的演示項目嗎? – matt 2014-09-23 13:27:18

回答

2

這似乎是一個針對iOS 8的錯誤給我。我遇到過同樣的問題。我的解決方案是禁用自動佈局預覽和訴諸autoresizingMask。

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

但我的預覽視圖是一個普通的UIImageView。我不知道如何使用一些複雜的基於自動佈局的預覽視圖。

我的設置與您的設置非常相似,我也在頂部使用了佔位符VC,但它也是使用autoresizingMask構建的,似乎更容易將事物垂直居中。

如果您在預覽視圖和體驗問題中使用了任何約束條件,那麼我會試着在另一個UIView中包裝預覽並查看它是否有效。 [view (autoresizingMask, custom frame, scaled)] -> [view (autoresizingMask = Flexible W+H)] -> [Preview (Autolayout)]

+0

謝謝!這個答案讓我朝着正確的方向前進。然而,不需要'autoresizingMask'。 – Sunkas 2014-09-24 13:31:25

+0

你是怎麼解決這個問題的。 – 2016-03-08 19:51:40