UIRefreshControl - beginRefreshing not working when UITableViewController is inside UINavigationController(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,然后将导航控制器添加为 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 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIRefreshControl - 当 UITableViewController 在 UINavigationController 内时,beginRefreshing 不起作用
				
        
 
            
        基础教程推荐
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
 - SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
 - Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
 - Android Volley - 如何动画图像加载? 2022-01-01
 - navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
 - iOS - UINavigationController 添加多个正确的项目? 2022-01-01
 - UIImage 在开始时不适合 UIScrollView 2022-01-01
 - 如何将图像从一项活动发送到另一项活动? 2022-01-01
 - Play 商店的设备兼容性问题 2022-01-01
 - 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				