2012-01-05 15 views
10

Quiero personalizar el encabezado de la sección TableView y dejar el color de fondo predeterminado. Yo uso - sección (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger). Necesito cambiar el tamaño de fuente dependiendo del dispositivo (iPad o iPhone). Para este propósito llamada siguiente función:¿Cuál es el color de fondo del encabezado de la sección TableView predeterminado en el iPhone?

[UIColor colorWithHue: 0,6 saturación: 0.33 brillo: 0,69 alpha: 0,6].

pero he encontrado estos valores manualmente.

Respuesta

23

La sección de encabezado color de fondo por defecto para tableview iOS7 es

Objective-C 
[UIColor colorWithRed:247/255.0f green:247/255.0f blue:247/255.0f alpha:1.0f] 

Swift 
UIColor(red: 247/255, green: 247/255, blue: 247/255, alpha: 1) 
+0

Como todos los componentes de color son idénticos (247/255), un cambio más simple native es: 'UIColor (blanco: 0.97, alfa: 1)' donde 0.97 ~ 247/255 –

0

Lo que he hecho en una de mis aplicaciones es la de crear el texto de esta manera:

NSString *sectionTitle = @"YOUR TITLE HERE"; 
CGSize sz = [sectionTitle sizeWithFont:[UIFont boldSystemFontOfSize:20.0f] 
        constrainedToSize:CGSizeMake(290.0f, 20000.0f) 
         lineBreakMode:UILineBreakModeWordWrap]; 

CGFloat height = MAX(sz.height, 20.0f); 

CGRect sectionFrame = CGRectMake(0.0, 0.0, 320.0, height); 

que utilizan 290 para el ancho de restricción para dar las fronteras de la etiqueta de cada lado. Luego utiliza una imagen estirable como esto:

UITableViewHeaderImage

y se escalan para que se ajuste el texto en el encabezado:

UIImage *headerImage = [UIImage imageNamed:@"sectionheaderbackground.png"]; 
    UIImage *stretchableImage = [headerImage stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:sectionFrame]; 
    backgroundImageView.image = stretchableImage; 

    // Add it to the view for the header 
    UIView *sectionView = [[UIView alloc] initWithFrame:sectionFrame]; 
    sectionView.alpha = 0.9; 
    sectionView.backgroundColor = [UIColor clearColor]; 
    [sectionView addSubview:backgroundImageView]; 

Por último, he creado una etiqueta y lo añadí como subvista:

CGRect labelFrame = CGRectMake(10.0, 0.0, 290.0, height); 
    UILabel *sectionLabel = [[UILabel alloc] initWithFrame:labelFrame]; 
    sectionLabel.text = sectionTitle; 
    sectionLabel.numberOfLines = 0; 
    sectionLabel.font = [UIFont boldSystemFontOfSize:18.0]; 
    sectionLabel.textColor = [UIColor whiteColor]; 
    sectionLabel.shadowColor = [UIColor grayColor]; 
    sectionLabel.shadowOffset = CGSizeMake(0, 1); 
    sectionLabel.backgroundColor = [UIColor clearColor]; 
    [sectionView addSubview:sectionLabel]; 

    return sectionView; 
3

para el cambio del fondo del color de TableView Sección Cabecera Justo cosas Línea añadir TableView delegado willDisplayHeaderView:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { 

    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view; 
    **header.tintColor = [UIColor whiteColor];** 

} 
1

Objective-C:

[UIColor colorWithRed:232/255.0f green:233/255.0f blue:237/255.0f alpha:1.0f] 

Swift:

UIColor(red: 232/255, green: 233/255, blue: 237/255, alpha: 1) 
+0

Esto es para iOS 9 y posterior –

Cuestiones relacionadas