FLKAutoLayout icon indicating copy to clipboard operation
FLKAutoLayout copied to clipboard

It may cause crash when using `FLKNoConstraint`

Open gonghao opened this issue 8 years ago • 4 comments

Here is what I am using:

button.alignTop("20", leading: FLKNoConstraint, bottom: FLKNoConstraint, trailing: "-27", toView: self.view)

In previous version, it works perfectly, but in 1.0 version, it cause crash.

gonghao avatar May 18 '16 11:05 gonghao

I try to fix it with this code:

#pragma mark Generic constraint methods for two views

- (NSLayoutConstraint *)alignAttribute:(NSLayoutAttribute)attribute toView:(id)view predicate:(NSString *)predicate
{
    NSArray *views = view ? @[view] : nil;
    NSLayoutConstraint *constraint = [UIView alignAttribute:attribute ofViews:@[self] toViews:views predicate:predicate].firstObject;
    return constraint ? constraint : [NSNull null];
}

- (NSLayoutConstraint *)alignAttribute:(NSLayoutAttribute)attribute toAttribute:(NSLayoutAttribute)toAttribute ofView:(id)view predicate:(NSString *)predicate
{
    NSArray *views = view ? @[view] : nil;
    NSLayoutConstraint *constraint = [UIView alignAttribute:attribute ofViews:@[self] toAttribute:toAttribute ofViews:views predicate:predicate].firstObject;
    return constraint ? constraint : [NSNull null];
}

gonghao avatar May 18 '16 12:05 gonghao

Interesting, I didn't know FLKNoConstraint existed, and I removed all nullability checks from 1.0.

I'd rather have consistent nonnull responses, so I'll remove it entirely.

orta avatar May 18 '16 12:05 orta

So I cannot use button.alignTop("20", leading: FLKNoConstraint, bottom: FLKNoConstraint, trailing: "-27", toView: self.view) in the future, and should I split these FLKNoConstraint to separate part like alignTopEdgeWithView and alignTrailingEdgeWithView ?

gonghao avatar May 18 '16 12:05 gonghao

Yep 👍

orta avatar May 18 '16 12:05 orta