Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Fixed incorrect bounds of snapshot when UIAppearance properties cause… #228

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions FBSnapshotTestCase/Categories/UIImage+Snapshot.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.