segmented how example control iphone uiview uiimage uisegmentedcontrol

iphone - how - swift 4 segmented control example



iPhone: UISegmentedControl con imágenes personalizadas en estado comprimido (2)

Estoy usando un UISegmentedControl con algunas imágenes personalizadas:

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?


Nic,

Después de jugar con esto por un tiempo, decidí simplemente "rodar el mío" 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.


Además de la respuesta de DexterW. Había tomado la misma decisión: imitar a UISegmentedControl usando solo UIButtons .
Sin embargo, no es una tarea simple imitar a UISegmentControl . Traté de usar el estado Selected de un UIButton pero UIButton no funcionará como se esperaba si establece la misma configuración (imagen bg, color de fuente) para los estados Hightlighted y Selected . En tal caso, cuando el segmento parece seleccionado, sigue siendo "pressable" visualmente. Entonces, para evitarlo y hacer que los botones se configuren más como UISegmentedControl , no UISegmentedControl ningún estado selected . En su lugar, Id decidió cambiar la apariencia del botón seleccionado en el código para que normal estados normal y highlighted sean los mismos visualmente 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 que usa la imagen bg llamada "segment_default" y color de fuente blanco y para el estado seleccionado su imagen bg llamada "segment_active" y la fuente negra. Entonces 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 es una mejor idea, pero funciona perfectamente para mí.