Add Datepicker in Uiactionsheet Using Swift

Add datepicker in UIActionsheet using Swift

It is not possible, but you can use a UItextfield input view and accessory view to show and dismiss a datepicker when the user clicks the textfield

class KPDatePickerViewController: UIViewController {
var datePicker:UIDatePicker!
@IBOutlet var dateTextField:UITextField!

override func viewDidLoad() {
var customView:UIView = UIView (frame: CGRectMake(0, 100, 320, 160))
customView.backgroundColor = UIColor.brownColor()
datePicker = UIDatePicker(frame: CGRectMake(0, 0, 320, 160))
customView .addSubview(datePicker)
dateTextField.inputView = customView
var doneButton:UIButton = UIButton (frame: CGRectMake(100, 100, 100, 44))
doneButton.setTitle("Done", forState: UIControlState.Normal)
doneButton.addTarget(self, action: "datePickerSelected", forControlEvents: UIControlEvents.TouchUpInside)
doneButton.backgroundColor = UIColor .blueColor()
dateTextField.inputAccessoryView = doneButton
}

func datePickerSelected() {
dateTextField.text = datePicker.date.description
}
}

How to add date picker to UIAlertController(UIAlertView) in Swift?

Do not use any kind of UIAlertView or UIAlertController. Make your own view and pop it up (using, probably, a presented view controller). The view can be small, like an date picker, and you can put a shadow view behind it, like an alert does.

How to add UIDatePicker in UIAlertController in iOS8?

Adding UIDatePicker to action sheet was discouraged by Apple all along. Since iOS 7, Apple had introduced the use of inline date picker (see how it is done in the Calendar app).

If you managed to hack a workaround using UIAlertController, it may probably break again in future iOS releases.

UIDatePicker inside actionsheet is not working

Finally I got the solution. This code works fine in device... Thanks for help

   UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];

UIDatePicker *datePicker = [[UIDatePicker alloc] init];

CGRect pickerFrame = datePicker.frame;
pickerFrame.size.height = datePicker.frame.size.height - 50;
datePicker.frame = pickerFrame;

[datePicker setDatePickerMode:UIDatePickerModeDate];
[alertController.view addSubview:datePicker];

UIAlertAction* alertAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {

}];

[alertController addAction:alertAction];

[self.view.window.rootViewController presentViewController:alertController animated:YES completion:nil];

Showing a UIPickerView with UIActionSheet in iOS8 not working

From the reference for UIActionSheet:

UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:.

My guess is your seeing exactly why.

The reference for UIAlertController doesn't have a similar disclaimer, but looking at its interface, my guess is Apple will add it before release.

My recommendation would be to just create a small view containing your picker and buttons and show and hide it as needed. It's not that hard to do and your not pushing interfaces beyond their intended uses.

UIDatePicker in UIActionSheet on iPad

I ended up creating a separate segment of code for the iPad Popover:

//build our custom popover view
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 344)];
popoverView.backgroundColor = [UIColor whiteColor];

datePicker.frame = CGRectMake(0, 44, 320, 300);

[popoverView addSubview:toolbar];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;

//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 244);

//create a popover controller
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];

//present the popover view non-modal with a
//refrence to the button pressed within the current view
[popoverController presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];

//release the popover content
[popoverView release];
[popoverContent release];


Related Topics



Leave a reply



Submit