Skip to content

Unintuitive behavior with autoPinEdges(toSuperviewMarginsExcludingEdge: .right) #209

@michaelkirk

Description

@michaelkirk

The common pattern of pinning three of four edges to the superview is nicely encapsulated in:
autoPinEdgesToSuperviewEdgesWithInsets:(ALEdgeInsets)insets excludingEdge:(ALEdge)edge

But the implementation only makes sense if you are using Leading/Trailing.

It doesn't make sense to mix notions of Left/Right with Leading/Trailing when positioning a single view, but in the current implementation if you specify (e.g.) "pin all edges except left", it will pin the "trailing" edge, but it should pin the "right" edge, since you're in the left/right world, not the leading/trailing.

For example, if I'm pinning all edges except the "Right" edge, then it should be the "Left" edge that is pinned, not the "Leading" edge.

Conversely if I'm pinning all edges except the "Left" edge, then it should the "Right" edge that is pinned, not the "Trailing" edge.

Current implementation for reference:

- (PL__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewEdgesWithInsets:(ALEdgeInsets)insets excludingEdge:(ALEdge)edge
{
    PL__NSMutableArray_of(NSLayoutConstraint *) *constraints = [NSMutableArray new];
    if (edge != ALEdgeTop) {
        [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:insets.top]];
    }
    if (edge != ALEdgeLeading && edge != ALEdgeLeft) {
        [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:insets.left]];
    }
    if (edge != ALEdgeBottom) {
        [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:insets.bottom]];
    }
    if (edge != ALEdgeTrailing && edge != ALEdgeRight) {
        [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:insets.right]];
    }
    return constraints;
}

Proposed changes (I can open a PR if it seems directionally agreeable):

- (PL__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewEdgesWithInsets:(ALEdgeInsets)insets excludingEdge:(ALEdge)edge
{
    PL__NSMutableArray_of(NSLayoutConstraint *) *constraints = [NSMutableArray new];
    if (edge != ALEdgeTop) {
        [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:insets.top]];
    }
    if (edge != ALEdgeBottom) {
        [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:insets.bottom]];
    }

   // Are we dealing with leading/trailing? Or left/right?
    if (edge == ALEdgeLeading) {
        [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:insets.right]];
    }
    if (edge == ALEdgeTrailing) {
        [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:insets.left]];
    }
    if (edge == ALEdgeLeft) {
        [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:insets.right]];
    }
    if (edge == ALEdgeRight) {
        [constraints addObject:[self autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:insets.left]];
    }

    return constraints;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions