UIRefreshControl - 当UITableViewController在UINavigationController中时,beginRefreshing无效 [英] UIRefreshControl - beginRefreshing not working when UITableViewController is inside UINavigationController

查看:464
本文介绍了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 添加到a首先是UINavigationController,然后将导航控制器添加为 rootViewController beginRefreshing 方法不再有效。例如,

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?

谢谢

推荐答案

似乎如果您以编程方式开始刷新,则必须自己滚动表视图,例如,通过更改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天全站免登陆