2015-07-30 26 views
6

Desidero centrare il contenuto del mio UITableView che contiene headerView e footerView creato allo storyboard e UITableViewCell s. Come posso raggiungere questo obiettivo?Come allineare verticalmente (al centro) il contenuto di UITableView?

Ecco cosa sto cercando di implementare per risolvere il mio problema, ma questo non funziona.

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    CGFloat height = self.tableView.frameHeight - self.navigationController.navigationBar.frameHeight - [UIApplication sharedApplication].statusBarFrame.size.height - (self.rowCount * self.rowHeight); 
    self.tableView.tableHeaderView.frameHeight = height/2.0; 
} 

Così ho sottratto l'altezza della barra di stato navigationBar & e le cellule altezza per l'altezza della tableView per ottenere l'altezza della zona vuota. Ora che ottengo l'altezza se l'area vuota, l'ho divisa in 2 per il footer e la vista dell'intestazione.

+0

serie di vista tabella dinamicamente –

+4

incollare un codice che cosa sei stato ancora provato? –

+0

@LalitKumar, ho aggiornato la domanda. Grazie. – Xchord

risposta

15

Nel viewWillAppear e nelle didRotateFromInterfaceOrientation funzioni:

CGFloat headerHeight = (self.view.frame.size.height - (ROW_HEIGHT * [self.tableView numberOfRowsInSection:0])))/2; 

self.tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, -headerHeight, 0); 

Questo risolverà il vostro problema.

EDIT

chiamata la funzioneupdateTableViewContentInset nelle viewWillLayoutSubviews e dopo ogni reloadData:

Ojective-C

- (void)updateTableViewContentInset { 
    CGFloat viewHeight = self.view.frame.size.height; 
    CGFloat tableViewContentHeight = self.tableView.contentSize.height; 
    CGFloat marginHeight = (viewHeight - tableViewContentHeight)/2.0; 

    self.tableView.contentInset = UIEdgeInsetsMake(marginHeight, 0, -marginHeight, 0); 
} 

Swift 4

func updateTableViewContentInset() { 
    let viewHeight: CGFloat = view.frame.size.height 
    let tableViewContentHeight: CGFloat = tableView.contentSize.height 
    let marginHeight: CGFloat = (viewHeight - tableViewContentHeight)/2.0 

    self.tableView.contentInset = UIEdgeInsets(top: marginHeight, left: 0, bottom: -marginHeight, right: 0) 
} 
+3

Le vostre soluzioni sono molto eleganti e pulite, non funziona nel caso abbiate un numero dinamico di celle. Se la tua tabella ha troppe celle, non verrà visualizzata correttamente. Quindi il seguente problema risolverà questo problema: 'headerHeight = MAX (0, headerHeight);' –

+1

Perché non usare 'self.tableView.rowHeight' invece di' ROW_HEIGHT'? –

-1

Set fotogramma del UITableView come questo

CGFloat width = self.view.frame.size.width; 
CGFloat height = self.view.frame.size.height; 
CGFloat tableHeight = 200.0; //your table height 
self.tableView.frame = CGRectMake(0,(height-tableHeight)/2,width,tableHeight); 

o provare questo

enter image description here

+1

Grazie, ma non voglio cambiare il frame del mio tableView poiché utilizzo l'autolayout. – Xchord

+0

ho aggiornato la mia risposta –

0

Aggiungi un UIView in alto (UIView dovrebbe essere genitore del vostro TableView) del vostro TableView da storyboard . Dopo di ciò imposta i suoi vincoli per riempire la vista. Ora, imposta le dimensioni del tuo TableView e imposta i suoi vincoli per centrare sia verticalmente che orizzontalmente il tuo top UIView. Questo risolverà il tuo problema.

0

Ho scoperto che cosa ha causato il mio calcolo errato. Il valore di self.tableView.bounds.size.height è diverso dall'altezza effettiva in viewWillAppear. Ho usato invece self.view.bounds.size.height.

1

Utilizzare la proprietà di contentSizeUITableView, così che non c'è bisogno di prendere il cellulare height o count in considerazione.

- (void) updateTableInsetsForHeight: (CGFloat) height 
{ 
    CGFloat tableViewInset = MAX((height - self.tableView.contentSize.height)/2.0, 0.f); 
    self.tableView.contentInset = UIEdgeInsetsMake(tableViewInset, 0, -tableViewInset, 0); 
} 

Aggiorna la contentInsets dopo aver ricaricato tableView -data, e quando la vista contenente del tableView diventa visibile (viewWillAppear:) e quando cambia dimensione (viewWillTransitionToSize:withTransitionCoordinator:).

- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator 
{ 
    [self updateTableInsetsForHeight: size.height]; 
} 
1

Swift 3 sulla base delle Pipiks rispondere

let headerHeight: CGFloat = (view.frame.size.height - CGFloat(Int(tableView.rowHeight) * tableView.numberOfRows(inSection: 0)))/2 
tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, -headerHeight, 0) 
-1

per SWIFT 3:

var headerHeight: CGFloat = (tableView.frame.size.height - CGFloat(Int(tableView.rowHeight) * tableView.numberOfRows(inSection: 0)))/2 
if headerHeight > 0 { 
    tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, 0/*-headerHeight*/, 0) 
} else { 
    headerHeight = 0 
    tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, 0/*-headerHeight*/, 0) 
} 

RISULTATO:

enter image description hereenter image description heretelaioenter image description hereenter image description here

Problemi correlati