floriankugler / FLKAutoLayout

UIView category which makes it easy to create layout constraints in code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to add Aspect Ratio

gilnuy opened this issue · comments

I set like this [qqBtn constrainAspectRatio:@"2:1"]; but it doesn't work!

- (NSArray*)constrainAspectRatio:(NSString*)predicate {
    return [self alignAttribute:NSLayoutAttributeWidth toAttribute:NSLayoutAttributeHeight ofView:self predicate:predicate];
}

will eventually reach:

NSLayoutConstraint* constraint = [NSLayoutConstraint constraintWithItem:self
                                                                  attribute:fromAttribute
                                                                  relatedBy:predicate.relation
                                                                     toItem:view
                                                                  attribute:toAttribute
                                                                 multiplier:predicate.multiplier
                                                                   constant:predicate.constant];
    if (predicate.priority) {
        constraint.priority = predicate.priority;
    }

Your predicate needs to be written in the same style as all other FLKAutoLayout predicates. Predicates consist of a relation, multiplier, constant and priority. @"2:1" is not a valid predicate and cannot be translated into a relation, multiplier, constant and priority.

See https://github.com/floriankugler/FLKAutoLayout#the-predicate-argument.

What you probably want is @"*2"

The term aspectRatio in the method signature doesn't refer to actual ratio notation, but rather indicates the purpose of the method which is to relate width to height. You could of course have an aspect ratio constraint like @">=*2.5@750" if you wanted to, which would mean:

width must be >= (2.5 * height) with a priority of 750.

👍