react actionsheet iphone ios uikit uiactionsheet ios7

iphone - actionsheet - uialertcontroller



UIActionSheet no muestra separador en el Ășltimo elemento en iOS 7 GM (8)

Probablemente sea un error en iOS7. Pero el último botón no está separado del anterior

Como puedes ver en la imagen. Esto sucede tanto en el simulador como en el dispositivo usando iOS7 GM. ¿Todos los demás tienen el mismo problema?

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"First", @"Second", @"Third", @"Fourth", nil]; [actionSheet showInView:self.view];

Como pueden ver, el código es bastante simple. ¿Alguna idea sobre cómo solucionar el problema? ¿O alguna biblioteca de terceros que puedo usar en lugar de UIActionSheet?


Creo que ActionSheet requiere un botón de cancelar. Así que puede agregar el título del botón Cancelar.

Otra forma es: Especifique cancelButtonIndex de actionSheet.

Por ejemplo, en su caso, puede agregar un " Cancelar " en otherButtonTitles en el índice 4 y luego especificar actionSheet.cancelButtonIndex = 4.


Descubrí que agregar un botón cancelar con una cadena vacía después de la inicialización funciona. El botón Cancelar no se mostrará y aparecerá el separador.

[sheet addButtonWithTitle: @""]; [sheet setCancelButtonIndex: sheet.numberOfButtons - 1];

Pero esto solo funciona para iPad. En el iPhone, aparece un botón de cancelar vacío, pero encontré una solución hacky para que funcione. Además de lo anterior, en willPresentActionSheet agregue este código en:

NSInteger offset = 55; CGRect superFrame = actionSheet.superview.frame; superFrame.origin.y += offset; [actionSheet.superview setFrame: superFrame]; // hide underlay that gets shifted with the superview [(UIView*)[[actionSheet.superview subviews] objectAtIndex: 0] removeFromSuperview]; // create new underlay CGRect underlayFrame = CGRectMake(0, -offset, superFrame.size.width, superFrame.size.height); UIView* underlay = [[UIView alloc] initWithFrame: underlayFrame]; underlay.alpha = 0.0f; [underlay setBackgroundColor: [UIColor colorWithWhite: 0.0f alpha: 0.4f]]; [actionSheet.superview insertSubview: underlay atIndex: 0]; // simulate fade in [UIView animateWithDuration: 0.3f animations:^{ underlay.alpha = 1.0f; }];

Esto desplaza hacia abajo la hoja para ocultar el botón cancelar de la pantalla


Encontré la manera de hacerlo funcionar en iPhone y iPad de la manera menos hacky:

  1. Solo inicie UIActionSheet con un título
  2. Agrega tus botones
  3. Agregue un botón "CANCELAR" por fin
  4. establezca CancelButtonIndex en ese último índice

Supongo que el separador faltante se debe a que el botón cancelar no se reconoce como un caso separado al agregarlo primero o a través del init.


La solución más simple es pasar @"" al título del botón cancelar en lugar de nil durante la asignación.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"" // change is here destructiveButtonTitle:nil otherButtonTitles:@"First", @"Second", @"Third", @"Fourth", nil]; [actionSheet showInView:self.view];


Parece que el initWithTitle: delegate: cancelButtonTitle: destructiveButtonTitle: otherButtonTitles: el método tiene errores y no debe especificar ningún botón de cancelación aquí. Por cierto, parece que un botón destructivo establecido en ese método tampoco funcionará muy bien.

En lugar de eso, debes:

  • proporcione un botón de cancelar personalizado como el último botón en la matriz de botones, por ejemplo: ["Action 1", "Action 2", "Close"] o sheet.addButtonWithTitle("Close")
  • establecer manualmente el índice del botón cancelar, por ejemplo, sheet.cancelButtonIndex = 2

Entonces todo funcionará como se espera. En el iPad, el botón se ocultará automáticamente y en el iPhone se diseñará y colocará correctamente.


Si tiene un botón cancelar, se mostrará la última fila. Esa es la solución temporal que estoy usando ahora. No conoce ninguna solución si no desea que se muestre un botón de cancelar


- (void)willPresentActionSheet:(UIActionSheet *)actionSheet { if ([UIDevice currentDevice].systemVersion.floatValue < 8.0f) { UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(8, 88, actionSheet.frame.size.width - 16, 0.5)]; separator.backgroundColor = [UIColor colorWithRed:219.0f/255 green:219.0f/255 blue:223.0f/255 alpha:1]; [actionSheet addSubview:separator]; } }

Cada botón tiene una altura 44. My actionSheet no tiene título. Y quería agregar un separador entre el segundo y tercer botones. Es por eso que uso el número 88.


UIActionSheet *asAccounts = [[UIActionSheet alloc] initWithTitle:Localized(@"select_an_account") delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil]; for (int i=0; i<[result count]; i++) { ACAccount *acct = [result objectAtIndex:i]; [asAccounts addButtonWithTitle:[acct username]]; asAccounts.tag = i; } [asAccounts addButtonWithTitle:Localized(@"Cancel")]; asAccounts.cancelButtonIndex = result.count; [asAccounts showInView:self.view];