How can I reveal the UIRefreshControl when I update the table programmatically? Using [self.refreshControl beginRefreshing] make the spinner animate but does not reveal it.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You'll have to manually change the contentOffset
of your UITableView
yourself. Be sure to account for the contentInset.top
. It should be something as simple as:
CGPoint newOffset = CGPointMake(0, -[myTableView contentInset].top);
[myTableView setContentOffset:newOffset animated:YES];
回答2:
This will do the trick
- (void)beginRefreshingTableView {
[self.refreshControl beginRefreshing];
// check if contentOffset is zero
if (fabsf(self.tableView.contentOffset.y) < FLT_EPSILON) {
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^(void){
self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);
} completion:^(BOOL finished){
}];
}
}
回答3:
For Swift 3, this is what I have based on Peter Lapisu's answer:
override func viewDidLoad() {
super.viewDidLoad()
self.refreshControl?.addTarget(self, action: #selector(refresh), forControlEvents: UIControlEvents.ValueChanged)
// ...
}
func refresh(sender:AnyObject) {
self.refreshControl?.beginRefreshing()
if let yOffsetTable = self.tableView?.contentOffset.y {
if yOffsetTable < CGFloat(Float.ulpOfOne) {
UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.beginFromCurrentState, animations: {
if let refreshControlHeight = self.refreshControl?.frame.height {
self.tableView?.contentOffset = CGPoint(x: 0, y: -refreshControlHeight)
}
}, completion: nil)
}
}
}