2011-06-01 29 views
9

Tengo un UIView que estoy configurando como propiedad de una propiedad UITableView tableFooterView. En los casos en que la vista de tabla y el pie de página no llenen toda la vista principal, ¿hay alguna manera de determinar cuánto más alto necesito para hacer el pie de página para que llene el espacio restante?¿Cómo puedo hacer que UITableView tableFooterView se expanda para llenar toda la vista principal?

Mi objetivo final aquí es conseguir que un botón de eliminar se alinee con la parte inferior de la vista. Si la vista de tabla es más grande que la vista principal, no voy a hacer nada y el botón Eliminar estará fuera de la vista para comenzar, lo cual está bien.

EDIT Esto debe funcionar en iPad en el tipo modal de hoja de formulario, donde los límites de vista solo deben ser los de la hoja de formulario, no toda la pantalla.

Respuesta

14

Extraño: dado que UITableViews son esencialmente UIScrollViews, intente utilizar el valor contentSize.height de su vista de tabla para ver cuánto ocupa la pantalla. Luego, ajuste el marco tableFooterView para llenar la diferencia de altura desde la altura del marco de su supervista. En esencia:

CGRect tvFrame = tableView.frame; 
CGFloat height = tvFrame.size.height - tableView.contentSize.height; 
if (height > MIN_HEIGHT) { // MIN_HEIGHT is your minimum tableViewFooter height 
    CGRect frame = tableFooterView.frame; 
    tableFooterView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height); 
} 
0

Una sola idea: si la vista de tabla tiene una altura de fila fija, puede multiplicar el número de filas por la altura de la fila más una cantidad fija. Si la altura de la fila no es fija, puede sumar todas las alturas.

0
CGRect parentFrame = myParentView.frame; //tells you the parents rectangle. 
    CGRect tableFrame = myTableView.frame; // tells you the tableView's frame relative to the parent. 

    float delta = parentFrame.size.height - tableFrame.size.height - tableFrame.origin.y; 

delta es la distancia entre la parte inferior de la tabla y la parte inferior de su vista de contenedor.

3

@ respuesta de octy trabajará para iOS 9. Sin embargo, para IOS 10 parece que contentSize del tableView incluye la altura tableViewFooter. En iOS 10 tuve que hacer algo como lo siguiente:

var rowDataBounds: CGRect { 
    get { 
     if numberOfSections <= 0 { 
      return CGRect(x: 0, y: 0, width: frame.width, height: 0) 
     } 
     else { 
      let minRect = rect(forSection: 0) 
      let maxRect = rect(forSection: numberOfSections-1) 
      return maxRect.union(minRect) 
     } 
    } 
} 

fileprivate func resizeFooterView(){ 

    if let footerView = tableFooterView { 

     var newHeight: CGFloat = 0 
     let tvFrame = self.frame; 

     if #available(iOS 10, *) { 

      newHeight = tvFrame.size.height - rowDataBounds.height - self.contentInset.bottom - self.contentInset.top 

     } 
     else { 

      newHeight = tvFrame.size.height - self.contentSize.height 

     } 
     if newHeight < 0 { 
      newHeight = 0 
     } 
     let frame = footerView.frame 
     if newHeight != frame.height { 
      footerView.frame = CGRect(x:frame.origin.x, y:frame.origin.y, width:frame.size.width, height: newHeight) 
     } 
    } 
} 

override func layoutSubviews() { 
    super.layoutSubviews() 
    resizeFooterView() 
} 
Cuestiones relacionadas