diff --git a/FBSnapshotTestCase/Categories/UIImage+Snapshot.m b/FBSnapshotTestCase/Categories/UIImage+Snapshot.m index 968091b..2f3a4c3 100644 --- a/FBSnapshotTestCase/Categories/UIImage+Snapshot.m +++ b/FBSnapshotTestCase/Categories/UIImage+Snapshot.m @@ -40,10 +40,6 @@ + (UIImage *)fb_imageForViewLayer:(UIView *)view + (UIImage *)fb_imageForView:(UIView *)view { - CGRect bounds = view.bounds; - NSAssert1(CGRectGetWidth(bounds), @"Zero width for view %@", view); - NSAssert1(CGRectGetHeight(bounds), @"Zero height for view %@", view); - // If the input view is already a UIWindow, then just use that. Otherwise wrap in a window. UIWindow *window = [view isKindOfClass:[UIWindow class]] ? (UIWindow *)view : view.window; BOOL removeFromSuperview = NO; @@ -55,9 +51,14 @@ + (UIImage *)fb_imageForView:(UIView *)view [window addSubview:view]; removeFromSuperview = YES; } + + [view layoutIfNeeded]; + CGRect bounds = view.bounds; + NSAssert1(CGRectGetWidth(bounds), @"Zero width for view %@", view); + NSAssert1(CGRectGetHeight(bounds), @"Zero height for view %@", view); + UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0); - [view layoutIfNeeded]; [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES]; UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext(); diff --git a/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/FBSnapshotTestCaseDemoTests.m b/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/FBSnapshotTestCaseDemoTests.m index 720866c..65994fc 100644 --- a/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/FBSnapshotTestCaseDemoTests.m +++ b/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/FBSnapshotTestCaseDemoTests.m @@ -63,6 +63,28 @@ - (void)testViewSnapshotWithUIAppearance FBSnapshotVerifyView(control, nil); } +- (void)testViewSnapshotWithUIAppearanceResizing +{ + // If this works properly you should be able to read the whole "Click me!" title in the snapshot + [[UIButton appearance] setContentEdgeInsets:UIEdgeInsetsMake(15, 15, 15, 15)]; + + UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; + [button setTitle:@"Click me!" forState:UIControlStateNormal]; + + button.translatesAutoresizingMaskIntoConstraints = NO; + [button addConstraint:[NSLayoutConstraint constraintWithItem:button + attribute:NSLayoutAttributeWidth + relatedBy:NSLayoutRelationGreaterThanOrEqual + toItem:nil + attribute:NSLayoutAttributeNotAnAttribute + multiplier:0 + constant:65]]; + [button sizeToFit]; + + self.usesDrawViewHierarchyInRect = YES; + FBSnapshotVerifyView(button, nil); +} + - (void)testViewSnapshotWithDifferentBackgroundColorPerArchitecture { UIColor *color = FBSnapshotTestCaseIs64Bit() ? [UIColor magentaColor] : [UIColor cyanColor]; diff --git a/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/ReferenceImages_32/FBSnapshotTestCaseDemoTests/testViewSnapshotWithUIAppearanceResizing@2x.png b/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/ReferenceImages_32/FBSnapshotTestCaseDemoTests/testViewSnapshotWithUIAppearanceResizing@2x.png new file mode 100644 index 0000000..cab71c0 Binary files /dev/null and b/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/ReferenceImages_32/FBSnapshotTestCaseDemoTests/testViewSnapshotWithUIAppearanceResizing@2x.png differ diff --git a/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/ReferenceImages_64/FBSnapshotTestCaseDemoTests/testViewSnapshotWithUIAppearanceResizing@2x.png b/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/ReferenceImages_64/FBSnapshotTestCaseDemoTests/testViewSnapshotWithUIAppearanceResizing@2x.png new file mode 100644 index 0000000..cab71c0 Binary files /dev/null and b/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/ReferenceImages_64/FBSnapshotTestCaseDemoTests/testViewSnapshotWithUIAppearanceResizing@2x.png differ diff --git a/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/ReferenceImages_64/FBSnapshotTestCaseDemoTests/testViewSnapshotWithUIAppearanceResizing@3x.png b/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/ReferenceImages_64/FBSnapshotTestCaseDemoTests/testViewSnapshotWithUIAppearanceResizing@3x.png new file mode 100644 index 0000000..2ce2c59 Binary files /dev/null and b/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/ReferenceImages_64/FBSnapshotTestCaseDemoTests/testViewSnapshotWithUIAppearanceResizing@3x.png differ