[英]How to pull to refresh without tableView?
I have next controller:我有下一个 controller:
final class CategoryBreakdownViewController: UIViewController {
private let scrollView = UIScrollView(alwaysBounceVertical: true)
var refreshControl = UIRefreshControl()
Where in viewDidLoad
I'm trying to implement pull to refresh without tableView
:在
viewDidLoad
我试图在没有tableView
的情况下实现 pull to refresh :
private func setupRefreshControl() {
scrollView.alwaysBounceVertical = true
scrollView.bounces = true
refreshControl.addTarget(self, action: #selector(updateView), for: .valueChanged)
self.scrollView.addSubview(refreshControl)
}
@objc func updateView() {
SyncScheduler.syncImmediately(
success: {
self.refreshControl.endRefreshing()
},
failure: { [weak self] errorMessage in
self?.present(message: errorMessage, style: .error)
self?.refreshControl.endRefreshing()
}
)
}
But in my case this solution doesn't worked, how to solve this problem?但在我的情况下,这个解决方案不起作用,如何解决这个问题?
There doesn't appear to be anything wrong with your approach.您的方法似乎没有任何问题。
Here is a complete example -- it adds a "full view" scrollView, with a 200-pt tall red subview (so we can see the effect).这是一个完整的示例——它添加了一个“全视图”滚动视图,带有一个 200 磅高的红色子视图(因此我们可以看到效果)。
When you drag down, you should see the UIRefreshControl
"spinner" appear.向下拖动时,您应该会看到
UIRefreshControl
“微调器”出现。 Drag down far enough, and it will call the updateView()
func.向下拖动足够远,它会调用
updateView()
函数。 Since we don't have your SyncScheduler
code, I added a 2-second async delay to simulate the process:由于我们没有您的
SyncScheduler
代码,因此我添加了 2 秒的异步延迟来模拟该过程:
final class CategoryBreakdownViewController: UIViewController {
private let scrollView = UIScrollView()
var refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
scrollView.backgroundColor = .systemTeal
scrollView.translatesAutoresizingMaskIntoConstraints = false
let testView = UIView()
testView.backgroundColor = .red
testView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(testView)
view.addSubview(scrollView)
let contentGuide = scrollView.contentLayoutGuide
let frameGuide = scrollView.frameLayoutGuide
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
testView.topAnchor.constraint(equalTo: contentGuide.topAnchor, constant: 20.0),
testView.leadingAnchor.constraint(equalTo: contentGuide.leadingAnchor, constant: 20.0),
testView.trailingAnchor.constraint(equalTo: contentGuide.trailingAnchor, constant: -20.0),
testView.bottomAnchor.constraint(equalTo: contentGuide.bottomAnchor, constant: -20.0),
testView.widthAnchor.constraint(equalTo: frameGuide.widthAnchor, constant: -40.0),
testView.heightAnchor.constraint(equalToConstant: 200.0),
])
setupRefreshControl()
}
private func setupRefreshControl() {
scrollView.alwaysBounceVertical = true
scrollView.bounces = true
refreshControl.addTarget(self, action: #selector(updateView), for: .valueChanged)
self.scrollView.addSubview(refreshControl)
}
@objc func updateView() {
// simulate a background process that takes 2 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0, execute: {
self.refreshControl.endRefreshing()
})
// SyncScheduler.syncImmediately(
// success: {
// self.refreshControl.endRefreshing()
// },
// failure: { [weak self] errorMessage in
// self?.present(message: errorMessage, style: .error)
// self?.refreshControl.endRefreshing()
// }
// )
}
}
So you are missing two line of code:所以你缺少两行代码:
self.scrollView.scrollEnabled = true
self.scrollView.alwaysBounceVertical = true
var alwaysBounceVertical: Bool
// default NO. var alwaysBounceVertical: Bool
// 默认 NO。 if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically.如果 YES 并且弹跳为 YES,即使内容小于边界,也允许垂直拖动。
Please try this.请试试这个。 Thanks
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.