example objective-c iphone cocoa-touch uidatepicker

objective-c - example - uidatepicker swift 4



Ajustar un UIDatePicker en una UIActionSheet (7)

Asegúrese de mostrar UIActionSheet en la vista derecha cuando se encuentre dentro de UITabBarController para evitar problemas de interactividad en la parte inferior:

[actionSheet showInView:self.parentViewController.tabBarController.view];

Estoy tratando de obtener un UIDatePicker con un UIButton para aparecer en una UIActionSheet. Desafortunadamente se corta y todo el Selector de Fecha no está visible. Ni siquiera he intentado agregar el UIButton todavía. ¿Alguien puede sugerir que la vista completa se ajuste correctamente? No estoy seguro de cómo agregar las dimensiones adecuadas ya que UIActionSheet parece carecer de un -initWithFrame: type constructor.

UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil]; // Add the picker UIDatePicker *pickerView = [[UIDatePicker alloc] init]; pickerView.datePickerMode = UIDatePickerModeDate; [menu addSubview:pickerView]; [menu showInView:self.view]; [pickerView release]; [menu release];

También he intentado con algo similar a:

UIActionSheet *menu = [[UIActionSheet alloc] initWithFrame:CGRectMake(200.0, 200.0, 100.0f, 100.0f)];

Por supuesto, los coords no son realistas, pero no parecen afectar la posición / tamaño de UIActionSheet.


Botones en la parte superior, selector en la parte inferior:

UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"OK",nil]; // Add the picker UIDatePicker *pickerView = [[UIDatePicker alloc] init]; pickerView.datePickerMode = UIDatePickerModeTime; [menu addSubview:pickerView]; [menu showInView:_mainView]; CGRect menuRect = menu.frame; menuRect.origin.y -= 214; menuRect.size.height = 300; menu.frame = menuRect; CGRect pickerRect = pickerView.frame; pickerRect.origin.y = 174; pickerView.frame = pickerRect; [pickerView release]; [menu release];


Esto funciona para mí

NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"/n/n/n/n/n/n/n/n/n" : @"/n/n/n/n/n/n/n/n/n/n/n/n" ; UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:[NSString stringWithFormat:@"%@%@", title, NSLocalizedString(@"SelectADateKey", @"")] delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Ok", nil]; [actionSheet showInView:self.view]; UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease]; datePicker.datePickerMode = UIDatePickerModeDate; [actionSheet addSubview:datePicker];

un poco complicado, pero funciona!


Intenta agregar la siguiente línea para resolver el problema de los botones desactivados

[menu sendSubviewToBack:pickerView];


La respuesta de Dmitry es casi correcta. Sin embargo, el marco de la hoja de acciones es demasiado pequeño y, por lo tanto, se ignoran los toques cerca de la parte inferior del selector. He corregido estos problemas:

UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"OK",nil]; // Add the picker UIDatePicker *pickerView = [[UIDatePicker alloc] init]; pickerView.datePickerMode = UIDatePickerModeTime; [menu addSubview:pickerView]; [menu showInView:_mainView]; CGRect menuRect = menu.frame; CGFloat orgHeight = menuRect.size.height; menuRect.origin.y -= 214; //height of picker menuRect.size.height = orgHeight+214; menu.frame = menuRect; CGRect pickerRect = pickerView.frame; pickerRect.origin.y = orgHeight; pickerView.frame = pickerRect; [pickerView release]; [menu release];


Posiblemente no responda directamente la pregunta específica (cómo hacer que UIDatePicker encaje), pero para una implementación de UIActionSheet que presente un UIDatePicker (también otros), consulte ActionSheetPicker . De su archivo Léame:

Presenta fácilmente una Hoja de Acción con un PickerView, lo que permite al usuario seleccionar entre una cantidad de opciones inmutables. Basado en la alternativa desplegable de HTML encontrada en mobilesafari.

También:

Algún código derivado de la publicación de marcio en Desbordamiento de pila [ Agregar UIPickerView y un botón en la hoja de acción - ¿Cómo? ]

Incluso si uno no usa el código, es una buena lectura antes de tirar el suyo.

Actualizar:

Esta versión está en desuso: use ActionSheetPicker-3.0 lugar.


Puede usar algo como esto (ajustar las coordenadas):

UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil]; // Add the picker UIDatePicker *pickerView = [[UIDatePicker alloc] init]; pickerView.datePickerMode = UIDatePickerModeDate; [menu addSubview:pickerView]; [menu showInView:self.view]; [menu setBounds:CGRectMake(0,0,320, 500)]; CGRect pickerRect = pickerView.bounds; pickerRect.origin.y = -100; pickerView.bounds = pickerRect; [pickerView release]; [menu release];

Pero es mejor que crees una vista de pantalla completa con un UIDatePicker y una barra de navegación. Para ver un ejemplo, vea UICatalog -> Ejemplo de Pickers desde el iPhone DevCenter.