[英]Swift Pull to Refresh with UIViewController without TableView or TableViewController
[英]Can I create a pull to refresh for UIViewController instead of UITableViewController in swift?
我有一个简单的应用程序,不需要用 UITableViewController 构建。 有没有机会为简单的 UIViewController 创建“拉动刷新”功能?
是的,有。 事实上,为了使用UIRefreshControl
只有一个要求 - 您可以将UIRefreshControl
放在UIScrollView
或其子类(例如UITableView
或UICollectionView
)中。
Swift 中的示例代码:
override func viewDidLoad() {
{
super.viewDidLoad()
//...
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(didPullToRefresh), forControlEvents: .ValueChanged)
scrollView.addSubview(refreshControl)
}
func didPullToRefresh() {
}
Objective-c 中的示例代码:
- (void)viewDidLoad
{
[super viewDidLoad];
//...
UIRefreshControl *refreshControl = [UIRefreshControl new];
[refreshControl addTarget:self action:@selector(didPullToRefresh) forControlEvents:UIControlEventValueChanged];
[self.scrollView addSubview:refreshControl];
}
- (void)didPullToRefresh
{
}
从 iOS 6 refreshControl
, UIScrollView
有一个refreshControl
属性(因此它是子类UITableView
和UICollectionView
)。
Apple 文档中的代码: https : //developer.apple.com/documentation/uikit/uirefreshcontrol#//apple_ref/occ/instm/UIRefreshControl
func configureRefreshControl () {
// Add the refresh control to your UIScrollView object.
myScrollingView.refreshControl = UIRefreshControl()
myScrollingView.refreshControl?.addTarget(self, action:
#selector(handleRefreshControl),
for: .valueChanged)
}
@objc func handleRefreshControl() {
// Update your content…
// Dismiss the refresh control.
DispatchQueue.main.async {
self.myScrollingView.refreshControl?.endRefreshing()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.