-
Notifications
You must be signed in to change notification settings - Fork 86
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
}() | ||
|
@@ -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 | ||
|
||
NSLayoutConstraint.activate([ | ||
refreshControl.centerXAnchor.constraint(equalTo: centerXAnchor), | ||
refreshControl.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor), | ||
refreshControl.heightAnchor.constraint(equalToConstant: refreshControlHeight) | ||
]) | ||
Comment on lines
+77
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
#endif | ||
} | ||
|
||
|
There was a problem hiding this comment.
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.