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

Fix refresh control positioning #123

Merged
merged 3 commits into from
May 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Source/Visitable/VisitableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ open class VisitableView: UIView {

open lazy var refreshControl: UIRefreshControl = {
let refreshControl = UIRefreshControl()
refreshControl.translatesAutoresizingMaskIntoConstraints = false
refreshControl.addTarget(self, action: #selector(refresh(_:)), for: .valueChanged)
return refreshControl
}()
Expand All @@ -68,6 +69,16 @@ open class VisitableView: UIView {

#if !targetEnvironment(macCatalyst)
scrollView.addSubview(refreshControl)

/// Infer refresh control's default height from its frame, if given.
/// Otherwise fallback to 60 (the default height).
let refreshControlHeight = refreshControl.frame.height > 0 ? refreshControl.frame.height : 60

Comment on lines +72 to +76
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have to specify the height?
Internally, refresh control doesn't use autolayout for positioning its subviews. Its main frame doesn't reflect its true size. The main container height is 60pt, but the view where the refresh image/animation is has a height of 100pt. When using autolayout the latter is used. 🤷‍♂️ Even if we don't set the height, the refresh control will respect the safe area, but it won't be positioned as it should be.

Screenshot 2023-05-17 at 11 52 53

NSLayoutConstraint.activate([
refreshControl.centerXAnchor.constraint(equalTo: centerXAnchor),
refreshControl.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
refreshControl.heightAnchor.constraint(equalToConstant: refreshControlHeight)
])
Comment on lines +77 to +81
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using autolayout ensures the system takes care of updating the view's position as appropriate.
centerXAnchor makes sure the control is always centered with the view’s horizontal center.
Pinning control's top anchor to the view's safe area top anchor ensures the control is positioned within the safe area.

#endif
}

Expand Down
Loading