UIRefreshControl - 当 UITableViewController 在 UINavigationController 中时,beginRefreshing 不起作用 [英] UIRefreshControl - beginRefreshing not working when UITableViewController is inside UINavigationController

查看:22
本文介绍了UIRefreshControl - 当 UITableViewController 在 UINavigationController 中时,beginRefreshing 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 UITableViewController(它在 UINavigationController 内部)中设置了一个 UIRefreshControl,它按预期工作(即下拉触发正确的事件).但是,如果我以编程方式调用刷新控件上的 beginRefreshing 实例方法,例如:

I've setup a UIRefreshControl in my UITableViewController (which is inside a UINavigationController) and it works as expected (i.e. pull down fires the correct event). However, if I programmatically invoke the beginRefreshing instance method on the refresh control like:

[self.refreshControl beginRefreshing];

什么都没发生.它应该向下动画并显示微调器.当我在刷新后调用 endRefreshing 方法时,它可以正常工作.

Nothing happens. It should animate down and show the spinner. The endRefreshing method works properly when I call that after the refresh.

我用这种行为创建了一个基本的原型项目,当我的 UITableViewController 直接添加到应用程序委托的根视图控制器时,它可以正常工作,例如:

I whipped up a basic prototype project with this behavior and it works properly when my UITableViewController is added directly to application delegate's root view controller, e.g:

self.viewController = tableViewController;
self.window.rootViewController = self.viewController;

但如果我先将 tableViewController 添加到 UINavigationController,然后将导航控制器添加为 rootViewControllerbeginRefreshing 方法不再有效.例如

But if I add the tableViewController to a UINavigationController first, then add the navigation controller as the rootViewController, the beginRefreshing method no longer works. E.g.

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
self.viewController = navController;
self.window.rootViewController = self.viewController;

我的感觉是这与导航控制器中嵌套的视图层次结构与刷新控件不兼容 - 有什么建议吗?

My feeling is this has something to do with the nested view hierarchies within the navigation controller not playing nice with the refresher control - any suggestions?

谢谢

推荐答案

似乎如果你开始以编程方式刷新,你必须自己滚动 table view,比如通过改变 contentoffset

It seems that if you start refreshing programmatically, you have to scroll the table view yourself, say, by changing contentoffset

[self.tableView setContentOffset:CGPointMake(0, -self.refreshControl.frame.size.height) animated:YES];

我猜这是因为当用户位于表格视图的中间/底部时滚动到刷新控件可能是不可取的?

I would guess the reason for this is that it could be undesirable to scroll to the refresh control when user is in the middle/bottom of the table view?

@muhasturk 的 Swift 2.2 版本

Swift 2.2 version by @muhasturk

self.tableView.setContentOffset(CGPoint(x: 0, y: -refreshControl.frame.size.height), animated: true)

简而言之,为了保持这种便携性,添加这个扩展

In a nutshell, to keep this portable add this extension

UIRefreshControl+ProgramaticallyBeginRefresh.swift

extension UIRefreshControl {
    func programaticallyBeginRefreshing(in tableView: UITableView) {
        beginRefreshing()
        let offsetPoint = CGPoint.init(x: 0, y: -frame.size.height)
        tableView.setContentOffset(offsetPoint, animated: true)        
    }
}

这篇关于UIRefreshControl - 当 UITableViewController 在 UINavigationController 中时,beginRefreshing 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆