2012-12-29 10 views

risposta

20

Dovrai modificare manualmente lo contentOffset del tuo UITableView. Assicurati di prendere in considerazione il numero contentInset.top. Dovrebbe essere qualcosa di semplice come:

CGPoint newOffset = CGPointMake(0, -[myTableView contentInset].top); 
[myTableView setContentOffset:newOffset animated:YES]; 
+3

I' Ho notato che questo tende a causare problemi con il disegno della cella se non resetti il ​​contentOffset anche dopo aver chiamato 'endRefreshing':' [myTableView setContentOffset: CGPointZero animato: YES]; ' –

+4

Non funziona in iOS8 – AlKozin

+2

Non funziona in ios 9. – cdub

17

Questo farà il trucco

- (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){ 

     }]; 

    } 
} 
+1

A quanto pare è necessario richiamare 'beginRefreshing' nel blocco di completamento dell'animazione tableview o dopo la chiamata dell'animazione, altrimenti il ​​tintColor del refreshControl impostato da te verrà ignorato. http://stackoverflow.com/a/20383030/903544 –

8

Per Swift 3, questo è quello che ho in base alla risposta di Peter Lapisu:

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) 
     } 
    } 
} 
Problemi correlati