Skip to content

Commit

Permalink
Fix crash when frame.origin has NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
cntrump committed Dec 22, 2021
1 parent a6ee531 commit 209393b
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions YogaKit/Source/UIView+Yoga.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ - (BOOL)isYogaEnabled {

static void YogaSwizzleInstanceMethod(Class cls, SEL originalSelector, SEL swizzledSelector);

static inline CGRect CorrectRectIfNeeded(CGRect rect) {
CGPoint origin = rect.origin;
CGSize size = rect.size;

#define CorrectValueIfNeeded(x) (isnan(x) ? 0 : x)

return CGRectMake(CorrectValueIfNeeded(origin.x),
CorrectValueIfNeeded(origin.y),
size.width, size.height);
}

@implementation UIView (YogaKitAutoApplyLayout)

+ (void)load {
Expand Down Expand Up @@ -74,6 +85,7 @@ - (BOOL)_yoga_isFittingSize {
#endif

- (instancetype)_yoga_initWithFrame:(CGRect)frame {
frame = CorrectRectIfNeeded(frame);
id _self = [self _yoga_initWithFrame:frame];
if (_self) {
[self _yoga_applyLayout];
Expand All @@ -83,6 +95,7 @@ - (instancetype)_yoga_initWithFrame:(CGRect)frame {
}

- (void)_yoga_setFrame:(CGRect)frame {
frame = CorrectRectIfNeeded(frame);
[self _yoga_setFrame:frame];
[self _yoga_applyLayout];

Expand All @@ -93,6 +106,7 @@ - (void)_yoga_setFrame:(CGRect)frame {
}

- (void)_yoga_setBounds:(CGRect)bounds {
bounds = CorrectRectIfNeeded(bounds);
[self _yoga_setBounds:bounds];
[self _yoga_applyLayout];

Expand Down

0 comments on commit 209393b

Please sign in to comment.