UIdate picker does not move freely - ios

I have a UIbutton on my UIViewController. Clicking that opens up an UIActionSheet with a UIDatePicker on it and a close button. I set the UIDatePicker to date mode .So it shows up with todays date. Suppose i want to select 2015 instead of 2013, i have to scroll down. But it won't allow me to scroll down. I have to scroll up once to 2012 and then scroll down. This seems like a minor issue but the client wants to scroll properly down. I checked on my personal phone to set alarm, i can freely scroll down. I don't have to scroll up and then scroll down. So this is what happens on button click.
- (IBAction)btnShowDateNeededClick:(UIButton *)sender
{
UIDatePicker *datePickerView = [[UIDatePicker alloc]init];
datePickerView.datePickerMode = UIDatePickerModeTime;
self.actSheet = [[ UIActionSheet alloc]init];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blueColor];
closeButton.tag = 101;
[closeButton addTarget:self action:#selector(dismissActionSheetWithTime:) forControlEvents:UIControlEventValueChanged];
[self.actSheet addSubview:closeButton];
[self.actSheet showInView:self.view];
[self.actSheet addSubview:datePickerView];
[self.actSheet sendSubviewToBack:datePickerView];
[self.actSheet setBounds:CGRectMake(0, 0, 320, 500)];
}
As you see, i have datePicker, segment control as a button added to UIActionsheet. Now do i have to change settings or date picker? If you need more information, please ask. Thanks.

Try This. may be this help.
In .h File.
UIDatePicker *datePickerView;
UIActionSheet *Actionsheet1;
In .m File
In viewDidLoad Method
// Picker Initialization
datePickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 0, 0)];
datePickerView.datePickerMode = UIDatePickerModeDateAndTime;
Make a function which will be called on your Button click
-(void) Create_Start_Date_Picker_Function
{
Actionsheet1 = [[UIActionSheet alloc] initWithTitle:nil delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
Actionsheet1.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[Actionsheet1 addSubview: datePickerView];
[Actionsheet1 showInView:self.view];
[Actionsheet1 setBounds:CGRectMake(0, 0, 320, 485)];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blueColor];
[closeButton addTarget:self action:#selector(Dismiss_Actionshit1) forControlEvents:UIControlEventValueChanged];
[Actionsheet1 addSubview:closeButton];
}
And in method for dismiss actionshit.
-(void) Dismiss_Actionshit1
{
// Your action sheet dismiss code
}

Related

iOS8 drawing rectangle not showing

I have the below code to draw a datepicker (showing around the middle of the screen). It works in iOS 7 but in iOS 8 it is not showing. Only the "Select Expiry Date" is shown in a small box in the bottom of the screen.
menu = [[UIActionSheet alloc] initWithTitle:#"Select Expiry Date"
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
pickerView = [[CDatePickerViewEx alloc] init];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = 50;
pickerRect.size.height = 100;
pickerView.frame = pickerRect;
UIButton *startbtn = [UIButton buttonWithType:UIButtonTypeCustom];
startbtn.frame = CGRectMake(240,12, 60, 25);
[startbtn setTitle:#"OK" forState:UIControlStateNormal];
[startbtn setBackgroundImage:[UIImage imageNamed:#"btn"] forState:UIControlStateNormal];
[startbtn addTarget:self action:#selector(pressedbuttonCancel:) forControlEvents:UIControlEventTouchUpInside];
[menu addSubview:pickerView];
[menu addSubview:startbtn];
[menu showInView:self];
[menu setFrame:CGRectMake(0,200,320, 260)];
Check if AutoLayout is enabled for your view, as well as Size classes. I ran into a similar issue before and remember of fixing it with programmatic constraints.

iPad Actionsheet not showing

I am trying to display action sheet in iPad which will contain 2 buttons and date pickerview.
I displayed the action sheet in iphone properly but when I try to display the action sheet in iPad its just displaying one thick black line.
Currently its looking like :
and code for displaying action sheet is
UIButton *btnClicked=(UIButton *)sender;
menu = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
/* menu=[[UIActionSheet alloc] initWithTitle:#"This will remove this recipe from all synced devices as well. Are you sure?" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Confirm Delete Recipe" otherButtonTitles:nil];*/
[menu setTag:121];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,5,300,20)];
[titleLabel setFont:[UIFont fontWithName:#"Trebuchet MS" size:18.0f]];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setNumberOfLines:1];
[menu addSubview: titleLabel];
UIButton *cancelButton = [UIButton buttonWithType: UIButtonTypeCustom];
cancelButton.frame = CGRectMake(30, 30, 124, 43);
[cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel_button.png"] forState: UIControlStateNormal];
[cancelButton addTarget:self action:#selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
cancelButton.adjustsImageWhenHighlighted = YES;
cancelButton.tag=33;
[menu addSubview: cancelButton];
UIButton *okButton = [UIButton buttonWithType: UIButtonTypeCustom];
okButton.frame = CGRectMake(170, 30, 124, 43);
[okButton setBackgroundImage:[UIImage imageNamed:#"continue_button.png"] forState: UIControlStateNormal];
[okButton addTarget:self action:#selector(okButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
okButton.adjustsImageWhenHighlighted = YES;
[menu addSubview: okButton];
datePickerView = [[UIDatePicker alloc] init];
CGRect pickerRect = datePickerView.bounds;
pickerRect.origin.y = 75;
datePickerView.frame = pickerRect;
//---Picker view for from date selection
if (btnClicked.tag==51)
{
[titleLabel setText:SELECT_FROM_DATE];
NSDate *now = [Global todayDate];
int daysToAdd = 1;
NSDate *newDate = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
// NSLog(#"%#",strDtFromDate);
datePickerView.minimumDate = newDate;
if (holidayType==2)
{
NSDate *holidayFromDate=[dateFormat dateFromString:objectSelectedGroup.fromDate];
// newDate=[holidayFromDate dateByAddingTimeInterval:60*60*24*daysToAdd];
if ([[Global todayDate] compare:holidayFromDate] == NSOrderedAscending)
{
datePickerView.minimumDate = holidayFromDate;
}
else if([[Global todayDate] compare:holidayFromDate] == NSOrderedDescending)
{
datePickerView.minimumDate = newDate;
}
NSDate *holidayToDate=[dateFormat dateFromString:objectSelectedGroup.toDate];
NSLog(#"%#",objectSelectedGroup.fromDate);
NSLog(#"%#",objectSelectedGroup.toDate);
datePickerView.maximumDate = holidayToDate;
}
okButton.tag=34;
}
else if(btnClicked.tag==52)
{
[titleLabel setText:SELECT_TO_DATE];
datePickerView.minimumDate=stringDate;
NSDate *holidayToDate=[dateFormat dateFromString:objectSelectedGroup.toDate];
datePickerView.maximumDate=holidayToDate;
okButton.tag=35;
}
datePickerView.datePickerMode = UIDatePickerModeDate;
stringDate=[datePickerView date];
[datePickerView addTarget:self action:#selector(selectedDateChanged:) forControlEvents:UIControlEventValueChanged];
[menu addSubview:datePickerView];
//menu.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
[menu showInView:self.view];
[menu setFrame:CGRectMake(0,self.view.frame.size.height - 290,320,290)];
And same code is working fine for iphone.
How to solve this?
Use PopOver Controller to display action sheet on ipad. To use actionsheet on Popover controller refer the following links,
UIDatePicker in UIActionSheet on iPad
UIActionSheet on iPad frame too small
UIActionaSheet is not fit for IPad and they most time behave unpredicted around frame
I would recommend a UIPopoverController with a custom UIViewController for doing this kinda user experience.
you can try replacing last two line with following lines,
[menu setFrame:CGRectMake(0,self.view.frame.size.height - 290,320,290)];
[menu showInView:[[[UIApplication sharedApplication] delegate] window]];

how to manage uipickerview in uiactionsheet

i am trying to show the UIPickerView in UIActionSheet . from the below mentioned code it works great in portrait mode but looks wired in Landscape Mode. (my app support iPhone 4/5 n landscape orientation also).
what i am doing wrong ?
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame=CGRectMake(0, 40, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"OK"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
Finally i got the solution
First of all i have to set the width of the picker view as i have mentioned
CGRect pickerFrame=CGRectMake(0, 40, 0, 0);
so it can be like
CGRect pickerFrame=CGRectMake(0, 40, 320, 0);
I found one more good solution :
Just call the picker view in action sheet by clicking a text field:
self.textField.inputView = yourCustomView;

Add a UIButton to UIDatePicker

I have a UITextField that I set the inputView to a UIDatePicker so that when the UITextField is pressed, instead of a keyboard, a UIDatePicker pops up. I would like to also add a toolbar with a 'Next' button on it so that the user could easily jump to the next field, but I am not sure how to do this as I am already altering a view on the UITextField to get the UIDatePicker. Here is the code used in loading the view:
- (void)viewDidLoad
{
self.datePicker = [[UIDatePicker alloc] init];
self.datePicker.datePickerMode = UIDatePickerModeDate;
[self.datePicker addTarget:self action:#selector(datePickerValueChanged) forControlEvents:UIControlEventValueChanged];
issue.inputView = self.datePicker;
}
Suggestions for getting the 'Next' button added to a toolbar on top of it?
Create a view to use as the text field's inputAccessoryView. This view should contain your buttons (e.g. Next, Cancel, Previous).
Here's the code :
if (keyboardToolbar == nil) {
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
[keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleDone target:self action:#selector(switchToNextField:)];
[keyboardToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, next, nil]];
}
issue.inputAccessoryView = keyboardToolbar;
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
issue.inputView = datePicker;
And in your switchToNextField method, just make the nextField as the firstResponder using [nextTextField becomeFirstResponder];
You can create a view then put any UI elements inside of it.
#property (nonatomic,strong) UIPopoverController *popoverControllerBirthday;
add above to your .h file and sync it at .m #synthesize popoverControllerBirthday;
also change your #synthesize datePicker; to #synthesize datePicker=_datePicker;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField==self.yourtextfield) {
UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];
_datePicker=[[UIDatePicker alloc]init];//Date picker
_datePicker.frame=CGRectMake(0,0,320, 216);
_datePicker.datePickerMode = UIDatePickerModeDate;
[_datePicker setMinuteInterval:5];
[_datePicker setTag:10];
[popoverView addSubview:_datePicker];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(result)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Next" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 220, 320, 45);
UIColor *titleColor = [UIColor whiteColor];
[button setTitleColor:titleColor forState:UIControlStateNormal];
// UIImage *doneButtonImage = [UIImage imageNamed:#"create event button.png"]; choose a button background if you ahve one
[button setBackgroundImage:doneButtonImage forState:UIControlStateNormal];
[popoverView addSubview:button];
popoverContent.view = popoverView;
popoverControllerBirthday = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverControllerBirthday.delegate=self;
CGRect myFrame =textField.frame;
myFrame.origin.x = 200;// your coordinates change it as you wish
myFrame.origin.y = 195;
[popoverControllerBirthday setPopoverContentSize:CGSizeMake(320, 265) animated:NO];
[popoverControllerBirthday presentPopoverFromRect:myFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
return NO; // tells the textfield not to start its own editing process (ie show the keyboard)
}
else{
return YES;
}
}
//handle your result call next uitextfield in your case
-(void) result
{
//in result method you can get date using datepicker.date
if (self.popoverControllerBirthday)
{
[self.popoverControllerBirthday dismissPopoverAnimated:NO];
self.popoverControllerBirthday = nil;
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"M d yyyy"];
NSString *parseDatePickerDAte =[dateFormatter stringFromDate:_datePicker.date]; // your string that is choosen at datepicker
// self.textfield.text = parseDatePickerDAte ; set this to your textfield if you want
//google this dont remember exact code for that, just changing your textfield to desired one
[self.nexttextfield becomesFirstResponder];
}
You can use SMDatePicker instead of UIDatePicker. It will create a custom view with UIToolbar and UIDatePicker. You can easy customise toolbar with necessary buttons. Take a look at example project.

Initial and place an UIButton in an UIActionSheet with an UIPickerView

I am implementing picker by Add UIPickerView & a Button in Action sheet - How?. I tried to replace the UISegmentedControl with a UIButton but it does not working. Could someone remind me why UIButton cannot be shown here?
Thank you.
- (IBAction) makeButtonPressed: (id) sender;
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
[actionSheet setTitle:#"Select Make"];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackOpaque];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.dataSource = self;
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[actionSheet addSubview:pickerView];
// UISegmentedControl *doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
// doneButton.momentary = YES;
// doneButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
// doneButton.segmentedControlStyle = UISegmentedControlStyleBar;
// doneButton.tintColor = [UIColor blackColor];
// [doneButton addTarget:self action:#selector(makeDone:) forControlEvents:UIControlEventValueChanged];
// [actionSheet addSubview:doneButton];
UIButton *doneButton = [[UIButton alloc] init];
doneButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
[doneButton addTarget:self action:#selector(makeDone:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:doneButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 460)];
}
The designated initializer for UIButton is +buttonWithType:. You should create your button with one of the predefined styles, or use UIButtonTypeCustom.
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundRect];
doneButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
[doneButton addTarget:self action:#selector(makeDone:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:doneButton];

Resources