2010-09-09 8 views
5

estoy usando un UISegmentedControl con algunas imágenes personalizadas:iPhone: UISegmentedControl con imágenes personalizadas en estado presionado

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:nil]; 
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"0.png"] atIndex:0 animated:NO]; 
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"1.png"] atIndex:1 animated:NO]; 
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"2.png"] atIndex:2 animated:NO]; 
segmentedControl.frame = CGRectMake(0, 0, 90, 30); 
[self.view addSubview:segmentedControl]; 
[segmentedControl release]; 

Esta parte funciona bien. Pero, todavía usa el estilo de Apple para el control y solo agrega mis imágenes sobre él. ¿Hay alguna manera de no tener que usar los estilos de Apple y personalizar el conrol con mis propias imágenes sin fondo? También me gustaría tener mis propias imágenes de estado "seleccionadas".

¿Posible?

Respuesta

3

Nic,

Después de jugar con esto por un tiempo, decidí simplemente "rodar mi propio" de una manera.

Hice algunas imágenes de botones en Photoshop y todo se veía como un control segmentado. Luego, configuré una imagen diferente para los estados "seleccionados" de todos los botones. Cuando se presionó un botón, llamé a setSelected: NO a los demás. Luego, manejé todo lo que tenía que hacer.

Me encantaría escuchar otras soluciones.

0

Además de la respuesta de DexterW. Llegué a la misma decisión: imitar UISegmentedControl usando UIButtons solamente.
Sin embargo, no es una tarea fácil de imitar UISegmentControl. Traté de usar Selected estado de un UIButton pero UIButton no funcionará como se esperaba si establece la misma configuración (imagen bg, color de fuente) para estados Hightlighted y Selected. En tal caso, cuando el segmento parece seleccionado, sigue siendo "pressable" visualmente. Entonces, para evitarlo y hacer que los botones actúen más como UISegmentedControl, no usé el estado selected. En cambio, Id decidió cambiar la apariencia del botón seleccionado en el código para que los estados normal y highlighted sean los mismos para el botón seleccionado y diferentes para uno no seleccionado.
Supongamos que tengo 3 botones segmentedControl (LeftSgm | MiddleSgm | RightSgm) y para el estado normal Im usando la imagen bg llamada "segment_default" y el color de fuente blanco y para el estado seleccionado su imagen BG llamada "segment_active" y la fuente negra. Así que en onTouchDown:

-(IBAction)onTopMenuTap:(id)sender 
{ 
    int newSelectedSegmentIndex; 
    if (sender == btnLeftSgm) 
     newSelectedSegmentIndex=0; 
    else if (sender == btnMiddleSgm) 
     newSelectedSegmentIndex=1; 
    else if (sender == btnRightSgm) 
     newSelectedSegmentIndex=2; 
    else 
     return; 

    NSLog(@"Tapped segment index %d while old one is %d",newSelectedSegmentIndex,selectedSegmentIndex); 

    if (newSelectedSegmentIndex==selectedSegmentIndex) 
     return; 

    [self handleSegmentAppearenace:newSelectedSegmentIndex]; 
//do whatever its need to be done 
... 
} 

y el método llamado es

-(void)handleSegmentAppearenace:(int)newSelectedSegmentIndex 
{ 
    if (selectedSegmentIndex==0){ 
     [btnLeftSgm setBackgroundImage:[UIImage imageNamed:@"segmented_control_default_left"] forState:UIControlStateNormal]; 
     [btnLeftSgm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
     btnLeftSgm.highlighted = NO; 
    } 
    else if (selectedSegmentIndex==1){ 
     [btnMiddleSgm setBackgroundImage:[UIImage imageNamed:@"segmented_control_default_center"] forState:UIControlStateNormal]; 
     [btnMiddleSgm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
     btnMiddleSgm.highlighted = NO; 
    } 
    else if (selectedSegmentIndex==2){ 
     [btnRightSgm setBackgroundImage:[UIImage imageNamed:@"segmented_control_default_right"] forState:UIControlStateNormal]; 
     [btnRightSgm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
     btnRightSgm.highlighted = NO; 
    } 

    if (newSelectedSegmentIndex==0){ 
     [btnLeftSgm setBackgroundImage:[UIImage imageNamed:@"segmented_control_active_left"] forState:UIControlStateNormal]; 
     [btnLeftSgm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
     btnLeftSgm.highlighted = YES; 
    } 
    else if (newSelectedSegmentIndex==1){ 
     [btnMiddleSgm setBackgroundImage:[UIImage imageNamed:@"segmented_control_active_center"] forState:UIControlStateNormal]; 
     [btnMiddleSgm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
     btnMiddleSgm.highlighted = YES; 
    } 
    else if (newSelectedSegmentIndex==2){ 
     [btnRightSgm setBackgroundImage:[UIImage imageNamed:@"segmented_control_active_right"] forState:UIControlStateNormal]; 
     [btnRightSgm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
     btnRightSgm.highlighted = YES; 
    } 
} 

Los tres botones están vinculados en IB a las propiedades correspondientes (btnRightSgm etc). También para "evento de retoque" en TopMenuTap está vinculado.
No puedo decir que sea una mejor idea pero me funciona perfectamente.

Cuestiones relacionadas