Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve alignment behavior around scroll views #54

Closed
NickEntin opened this issue Feb 22, 2021 · 2 comments
Closed

Improve alignment behavior around scroll views #54

NickEntin opened this issue Feb 22, 2021 · 2 comments
Assignees
Labels
bug Something isn't working as expected
Milestone

Comments

@NickEntin
Copy link
Collaborator

@frozendevil recently pointed out an issue with the way alignment works when using scroll views. Specifically, imagine you have a content view inside a scroll view, with a layout like this:

scrollView.alignToSuperview(.topCenter)
contentView.align(.topCenter, with: scrollView, .topCenter)

This will only work correctly when the scroll view has a content offset of zero.

I think we can fix this as part of the move to aligning via bounds and center (#49). Specifically, the alignment methods should treat the receiver view and the other view (the parameter) differently. The view being aligned (the receiver) should be aligned by the rect made from its bounds.size, center, and layer.anchorPoint to the rect of the target view (the parameter) by the rect made from its bounds.size, bounds.origin, center, and layer.anchorPoint.

@NickEntin NickEntin added this to the 1.0 milestone Feb 22, 2021
@NickEntin NickEntin self-assigned this Feb 22, 2021
@NickEntin
Copy link
Collaborator Author

One consideration here is that UIView.point(at:) becomes ambiguous with this changes. I think the best option for this is to change the name back to point(inBoundsAt:) to make it clear which rect it's using. I don't think we need to add a method for the other rect, but we can considering adding it in the future if needed.

@NickEntin NickEntin added the bug Something isn't working as expected label Mar 20, 2021
@NickEntin
Copy link
Collaborator Author

NickEntin commented Nov 22, 2021

Thinking more about this, it's a bit more complicated than including the target view's bounds.origin in the alignment computation. Imagine the following view hierarchy:

Container View
| Scroll View
| | Content View
| | | Label
| Tooltip View

If we aligned the content view to the scroll view, we'd want to use it's bounds.origin. Same thing with the label. But if we aligned the tooltip to the scroll view, we wouldn't want the bounds.origin. There are also some other weird edge cases, like what happens if we align the scroll view to the content view?

I think the best approach here is to make the behavior explicit in the API. Something like this might work:

enum TargetAlignmentBehavior {

    /// Align to the position in the target view's frame. This is most commonly used when aligning sibling
    /// views in a hierarchy.
    case untransformedFrame

    /// Align to the position in the target view's untransformed bounds. This is most commonly used when
    /// aligning a view to a view in its superview chain.
    case untransformedBounds

    /// Align views based on their relationship in the view hierarchy. If the target view is in the
    /// superview chain of the view being aligned, this will align to the target view's untransformed
    /// bounds. Otherwise it will align to the target view's untransformed frame.
    case automatic

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working as expected
Projects
None yet
Development

No branches or pull requests

1 participant