I need to add a toolbar with done button on the top of UIPickerView. I don't want to use actionSheet because I want the rest of the view to be active. I've gone for the following code:
- (BOOL)textFieldDidBeginEditing:(UITextField *)textField {
[txtstate resignFirstResponder];
pickerView = [[UIPickerView alloc]init] ;
pickerView.frame=CGRectMake(10, 75, 180,20);
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0,75,180,10);
toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(doneClicked:)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
textField.inputAccessoryView = toolbar;
[pickerView addSubview:toolbar];
[self.view addSubview:pickerView];
}
By using the above code my toolbar is added but it is hidden under UIPickerView and it is getting added in the middle. How can I make the toolbar come to the front (on the top of UIPickerView) ?
There is a simpler way
You can insert the picker as inputView of your text Field
and add the toolbar as inputAccessoryView to your textField
like:
textField.inputView = pickerView;
textField.inputAccessoryView = toolBar;
You can getPicker using this
-(void)getValuePicker
{
ViewForValuePicker = [[UIView alloc]initWithFrame:CGRectMake(0, 219, 320, 266)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneBtnPressToGetValue)];
[toolBar setItems:[NSArray arrayWithObject:btn]];
[ViewForValuePicker addSubview:toolBar];
valuePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
valuePicker.delegate=self;
valuePicker.dataSource=self;
valuePicker.showsSelectionIndicator=YES;
[ViewForValuePicker addSubview:valuePicker];
[appDelegate.window addSubview:ViewForValuePicker];
}
And its Delegete Method
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [pickerValueAry count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
NSMutableArray *ary = [[NSMutableArray alloc] initWithArray:pickerValueAry];
id str=[ary objectAtIndex:row];
return str;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"selectedRowInPicker >> %d",row);
}
You can follow my answer for more Link
in viewDidLoad just add this code and when textFieldBegin just change frame like bellow...
First Create global UIView For back View of Date Picker in .h file like bellow..
UIView *viewPicker;
UIPickerView * pickerView;
and then use it like bellow..
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
viewPicker.frame = CGRectMake(0, 112, 320, viewPicker.frame.size.height);
[UIView commitAnimations];
return YES;
}
add this bellow code...
viewPicker = [[UIView alloc]initWithFrame:CGRectMake(0, 480, 320, 258)];
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0,0,320,44);
toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(doneClicked:)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
pickerView = [[UIPickerView alloc]init] ;
pickerView.frame=CGRectMake(0, 44, 320,216);
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[viewPicker addSubview:pickerView];
[viewPicker addSubview:toolbar];
[self.view addSubview:viewPicker];
and in Done button clicked just set again frame like this..
-(IBAction)doneClicked:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
viewPicker.frame = CGRectMake(0, 370, 320, 258);
[UIView commitAnimations];
}
i hope this help you...
You could try with this code:
- (BOOL)textFieldDidBeginEditing:(UITextField *)textField {
[textField resignFirstResponder];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 180, 260)];
pickerView = [[UIPickerView alloc]init] ;
pickerView.frame=CGRectMake(0, 44, 180,216);
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0,0,180,44);
toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(doneClicked:)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
textField.inputAccessoryView = toolbar;
[view addSubview:toolbar];
[view addSubview:pickerView];
[self.view addSubview:view];
return YES;
}
Ok... How about just creating a vertical stack view on the storyboard editor, adding the toolbar above and the pickerview below? They don't have to be really associated to each other, they just need to be displayed and hidden at the same time. Kinda like this below:
It is really easy to do and you don't have to do coding magic.
Yeah I know this question is 7 years old but there are still people looking for solutions to this question.
you can set your UIPickerView like this way:-
-(void)showPicker{
menu = [[UIActionSheet alloc] initWithTitle:#"Select Time"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
pickerView=[[UIPickerView alloc] init];
pickerView.frame= CGRectMake(0.0, 44.0, 0.0, 0.0);
pickerView.delegate=self;
pickerView.dataSource=self;
pickerView.showsSelectionIndicator = YES;
txtopsbox.inputView=pickerView;
dateTool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
dateTool.barStyle=UIBarStyleBlackOpaque;
[dateTool sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(DatePickerDoneClick)];
[barItems addObject:flexSpace];
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelClicked:)];
[barItems addObject:cancel];
[dateTool setItems:barItems animated:YES];
[menu addSubview:dateTool];
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 464)];
[pickerView release];
}
UPDATE
You can also do like Another Way like:-
- (IBAction)showPicker
{
if(pickerVisible == NO)
{
// create the picker and add it to the view
if(self.datePicker == nil) self.datePicker = [[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 460, 320, 216)] autorelease];
[self.datePicker setMaximumDate:[NSDate date]];
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
[self.datePicker setHidden:NO];
[self.view addSubview:datePicker];
// the UIToolbar is referenced 'using self.datePickerToolbar'
[UIView beginAnimations:#"showDatepicker" context:nil];
// animate for 0.3 secs.
[UIView setAnimationDuration:0.3];
CGRect datepickerToolbarFrame = self.datePickerToolbar.frame;
datepickerToolbarFrame.origin.y -= (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
self.datePickerToolbar.frame = datepickerToolbarFrame;
CGRect datepickerFrame = self.datePicker.frame;
datepickerFrame.origin.y -= (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
self.datePicker.frame = datepickerFrame;
[UIView commitAnimations];
pickerVisible = YES;
}
}
- (IBAction)done
{
if(pickerVisible == YES)
{
[UIView beginAnimations:#"hideDatepicker" context:nil];
[UIView setAnimationDuration:0.3];
CGRect datepickerToolbarFrame = self.datePickerToolbar.frame;
datepickerToolbarFrame.origin.y += (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
self.datePickerToolbar.frame = datepickerToolbarFrame;
CGRect datepickerFrame = self.datePicker.frame;
datepickerFrame.origin.y += (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
self.datePicker.frame = datepickerFrame;
[UIView commitAnimations];
// remove the picker after the animation is finished
[self.datePicker performSelector:#selector(removeFromSuperview) withObject:nil afterDelay:0.3];
}
}
Related
I made a textfield who's first responder is a datePicker. I also attached a toolBar to to a view, so when the textfield is tapped, the toolbar slides up with an animation. Then on the toolBar their is a done button that resigns the first responder. I also want it to remove the toolBar. To do this I added this
[pickerBar removeFromSuperview];
Then to reopen it I did this
[self.view addSubview:pickerBar];
So it is activated when the textview is touched.
The problem is when I tap the textfield again the toolBar loses its navigation.
Heres most of the code
pickerBar = [[UIToolbar alloc] init];
pickerBar.barStyle=UIBarStyleBlackOpaque;
[pickerBar sizeToFit];
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0) {
CGRect pickerBarRect = CGRectMake(0, 568, 320, 44);
[pickerBar setFrame:pickerBarRect];
} else {
CGRect pickerBarRect = CGRectMake(0, 480, 320, 44);
[pickerBar setFrame:pickerBarRect];
}
//Set up the new position of the frame of the view
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(releasePicker)];
[barItems addObject:doneBtn];
[barItems addObject:doneBtn];
[pickerBar setItems:barItems animated:YES];
}
}
}
- (void)releasePicker {
[textfield resignFirstResponder];
[pickerBar removeFromSuperview];
}
- (void)pickerActivated {
[UIView animateWithDuration:.4 animations:^(void) {
[self.view addSubview:pickerBar];
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0){
CGRect rect = CGRectMake(0, 266, 320, 44);
[pickerBar setFrame:rect];
pickerBar.hidden = NO;
} else {
CGRect rect = CGRectMake(0, 178, 320, 44);
[pickerBar setFrame:rect];
}
}];
}
There is a difference between inputView and inputAccessoryView. This is what you want:
// setting the inputView
UIPickerView *pickerView = [[UIPickerView alloc] init];
// other configurations //
self.textField.inputView = pickerView;
// setting up the inputAccessoryView
UIToolbar *pickerBar = [[UIToolbar alloc] init];
pickerBar.barStyle = UIBarStyleBlackOpaque;
[pickerBar sizeToFit];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(releasePicker)];
[pickerBar setItems:#[flexSpace, doneBtn] animated:YES];
self.textField.inputAccessoryView = pickerBar;
This way the tool bar will be "attached" to your inputView and will automatically come up when the the text field is activated and disappear when your picker does!
Edit/Update
I did find this link that may help you, if you are interested in using a IB focused approach:
https://stackoverflow.com/a/10705161/1429262
Far simpler is to use the inputAccessoryView property on UITextField. It is irrelevant whether your inputView is a keyboard or a picker. Set your toolbar as the inputAccessoryView and it will animate in on top of your picker with the done button and then animate out on resignFirstResponder.
Im having 3 textfield in one i have implemented a UIPickerView and disable the keyboard using resignFirstResponder , but when i click on the other, both Keyboard and pickerview are visible .how do i sort this ?
create local instance of UIPickerView, and in delegate method of TextFiled textFieldDidBeginEditing check for visibility of UIPickerView, if it is Visible hide it
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(![_pickerView isHidden])
{
[_pickerView setHidden:YES];
}
//...
}
set every UITextField's Delegate to self and then just put this method in your .m class.. In this code i just set frame of yourPickerView with 500 y point instead of that you can Hide the UIPickerView..
This code Hides UIPickerView with Animation like Keyboard..
UPDATE:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == TextField1) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect frame = yourPickerView.frame;
frame.origin.y = 500;
yourPickerView.frame = frame;
[UIView commitAnimations];
return YES;
}
else if (textField == TextField2) {
[TextField1 resignFirstResponder];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect frame = yourPickerView.frame;
frame.origin.y = 107;
yourPickerView.frame = frame;
[UIView commitAnimations];
return NO;
}
return YES;
}
You need to know what to show exactly. You can use [self.view endEditing:YES]; in order to remove the keyboard when necessary, and implement your own method to hide the picker when needed.
Set the picker as the inputView for the textfield:
myTextField.inputView = self.myPickerView;
This way you don't need to handle the displaying of the keyboard/picker at all. It's all done for you.
Simplest Solution : inputView Property
yourTextField.inputView = self.yourPickerView;
Check the Documentation.
You need to Put this line First when you are going to Display the PickerView.
[self.view endEditing:YES];
Now after that PickerView Will open & focus will not be on the TextField after writing following line so what you need to do is Put Done Button over Picker & pass the Selected value of Picker to that TextField.
Try this Code:
- (void)textFieldDidBeginEditing:(UITextField *)aTextField
{
if(txtVisitDate.isEditing == TRUE)
{
if ( [txtPlaceName isFirstResponder] )
{
[txtPlaceName resignFirstResponder];
}
[aTextField resignFirstResponder];
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 475)];
}
}
Hope this will help.
I m newbie to objective-C
I want to display the UIDatePicker in a popup window after the button click ..
I have a button and when I click the button my popup should appear with DatePicker and later after chosing the date the popup should close and set the selected date in a textbox.
How can I do this ?
To create a datepicker I wrote this code ..
UIDatePicker *datePicker=[[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode=UIDatePickerModeDate;
[self.view addSubview:datePicker];
But I do not know how to display it in a popup window on a button click ..?
Declaration in your .h file
UIActionSheet *aac;
UIDatePicker *theDatePicker;
Implementation in .m file
// Add the code after your comment
-(void)DatePickerDoneClick:(id)sender {
NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = #"MM/dd/yyyy";
NSArray *temp=[[NSString stringWithFormat:#"%#",[df stringFromDate:theDatePicker.date]] componentsSeparatedByString:#""];
[dateString1 release];
dateString1=nil;
dateString1 = [[NSString alloc]initWithString:[temp objectAtIndex:0]];
UITextField* BirthDayTxtLBl.text = [NSString stringWithFormat:#" %#",dateString1];
NSString *theTime = [NSString stringWithFormat:#"%#",BirthDayTxtLBl.text];
NSLog(#"%#",theTime);
[aac dismissWithClickedButtonIndex:0 animated:YES];
}
- (void)DatePickercancelClick:(id)sender{
[aac dismissWithClickedButtonIndex:0 animated:YES];
}
-(IBAction)AddTheTimePicker:(id)sendar {
aac = [[UIActionSheet alloc] initWithTitle:[self isViewPortrait]?#"\n\n":nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
theDatePicker.datePickerMode=UIDatePickerModeDateAndTime;
UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:[self isViewPortrait]?CGRectMake(0, 0, 320, 44):CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(DatePickerDoneClick:)];
//doneBtn.tag = tagID;
[barItems addObject:doneBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UILabel *toolBarItemlabel;
if([self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown || [self interfaceOrientation] == UIInterfaceOrientationPortrait)
toolBarItemlabel= [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180,30)];
else
toolBarItemlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200,30)];
[toolBarItemlabel setTextAlignment:UITextAlignmentCenter];
[toolBarItemlabel setTextColor:[UIColor whiteColor]];
[toolBarItemlabel setFont:[UIFont boldSystemFontOfSize:16]];
[toolBarItemlabel setBackgroundColor:[UIColor clearColor]];
toolBarItemlabel.text = [NSString stringWithFormat:#"Select Start Time"];
UIBarButtonItem *buttonLabel =[[UIBarButtonItem alloc]initWithCustomView:toolBarItemlabel];
[toolBarItemlabel release];
[barItems addObject:buttonLabel];
[buttonLabel release];
[barItems addObject:flexSpace];
UIBarButtonItem *SelectBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(DatePickercancelClick:)];
[barItems addObject:SelectBtn];
[pickerDateToolbar setItems:barItems animated:YES];
[aac addSubview:pickerDateToolbar];
[aac addSubview:theDatePicker];
CGRect myImageRect = CGRectMake(0.0f, 300.0f, 320.0f, 175.0f);;
[aac showFromRect:myImageRect inView:self.view animated:YES ];
[UIView beginAnimations:nil context:nil];
if([self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown || [self interfaceOrientation] == UIInterfaceOrientationPortrait)
[aac setBounds:CGRectMake(0,0,320, 464)];
else
[aac setBounds:CGRectMake(0,0,480, 400)];
[UIView commitAnimations];
}
// Add this if you wish to add support Orientation support for picker
- (BOOL) isViewPortrait {
UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
return (currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
Try this, this can help you
-(IBAction)DatePickerAction:(id)sender
{
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];
}
you can set the .inputView property of the text field to a datepicker..
so myTextField.inputView = datePicker;
this will replace the keyboard input view for the datePicker.
Gets a little more indepth, you will need to add a target for when the value of the date picker changes (so it updates the text field)
let me know if this is how you want to go about it.
If you still want the popup window im not sure if you can subclass UIAlertView for this..
or you can add the datePicker to a UIView instance and add that onto the screen with some fancy animation, etc....
Plenty options and im happy to help you with them...
Write in your Button Click method
self.DatePicker= [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
self.DatePicker.datePickerMode = UIDatePickerModeDate;
[self.DatePicker addTarget:self
action:#selector(SetDatePickerTime:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.DatePicker];
- (void)SetDatePickerTime:(id)sender
{
[self.DatePicker removeFromSuperview];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"dd:MM:yyyy"];
NSLog(#"%#",[outputFormatter stringFromDate:self.DatePicker.date]);
}
Best method will be to create animation like it..ive tried it using extension to uiview..
try the following
in the .m file where you want to show popup just above the normal implementation
#interface UIView (AlertAnimation)
- (void)doPopInAnimation;
#end
const NSTimeInterval kAnimationDuration = 0.3f;
#implementation UIView (AlertAnimation)
- (void) doPopInAnimation {
CALayer *viewLayer = self.layer;
CAKeyframeAnimation *popInAnimation = [CAKeyframeAnimation animationWithKeyPath:#"transform.scale"];
popInAnimation.duration = 0.3f;
popInAnimation.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.6], [NSNumber numberWithFloat:1.2], [NSNumber numberWithFloat:0.9], [NSNumber numberWithFloat:1], nil];
popInAnimation.keyTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.6], [NSNumber numberWithFloat:0.8], [NSNumber numberWithFloat:1.0], nil];
[viewLayer addAnimation:popInAnimation forKey:#"transform.scale"];
}
#end
further put this magical line on the event you want popup
[yourPickerView doPopInAnimation];
Refer this code
startDatePicker = [[UIDatePicker alloc] init];
startDatePicker.datePickerMode = UIDatePickerModeDate;
[startDatePicker addTarget:self action:#selector(startLabelChange:)forControlEvents:UIControlEventValueChanged];
CGSize pickerSize = [startDatePicker sizeThatFits:CGSizeZero];
startDatePicker.frame = CGRectMake(-40, -20, pickerSize.width, pickerSize.height);
startDatePicker.transform = CGAffineTransformMakeScale(0.750f, 0.750f);
UIViewController *pickerController = [[UIViewController alloc] init];
[pickerController.view addSubview:startDatePicker];
[startDatePicker release];
[pickerController setContentSizeForViewInPopover:CGSizeMake(240, 180)];
UIPopoverController *pickerPopover = [[UIPopoverController alloc] initWithContentViewController:pickerController];
[pickerPopover presentPopoverFromRect:startDatelabel.frame
inView:rightSideView
permittedArrowDirections:UIPopoverArrowDirectionLeft
animated:YES];
pickerPopover.delegate = self;
self.popover = pickerPopover;
[pickerController release];
[pickerPopover release];
I have the code below in my app. I show a UIPickerView with options, and I have a TextView placed on the same UIViewController .
when the picker is shown with options, the rest of the view seems not in focus and only when I resign the uipicker, the entire view gets focus.
what is the problem here?
-(void)viewWillAppear:(BOOL)animated {
myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIPickerView *pickerViewCountry = [[UIPickerView alloc] init];
pickerViewCountry.showsSelectionIndicator = YES;
pickerViewCountry.dataSource = self;
pickerViewCountry.delegate = self;
pickerViewCountry.frame = CGRectMake(0,35 , 320, 15);
[myActionSheet addSubview:pickerViewCountry];
[pickerViewCountry release];
closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 27.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(hidePicker) forControlEvents:UIControlEventValueChanged];
[myActionSheet addSubview:closeButton];
[closeButton release];
[myActionSheet showInView:self.view];
[myActionSheet setBounds:CGRectMake(0, 0, 320, 250)];
UITextView *myTextView = [[UITextView alloc] init];
myTextView.frame = CGRectMake(100, 12, 210, 20);
myTextView.layer.borderWidth = 1.0f;
myTextView.layer.cornerRadius = 5;
myTextView.clipsToBounds = YES;
myTextView.layer.borderColor = [[UIColor grayColor] CGColor];
myTextView.text = #"some text";
[self.view addSubview:myTextView];
[myTextView sizeToFit];
}
You've placed your UIPickerView inside UIActionSheet. That's standart behavior for UIActionSheet. Try to use keyboardView instead like so:
UITextField* dummyTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, -20, 10, 10)];//the point is to put it outside visible view
dummyTextField.inputView = pickerViewCountry;
[dummyTextField becomeFirstResponder];//call to appear
[dummyTextField resignFirstResponder];//call do disappear
and for the buttons you can use folloving:
dummyTextField.inputAccessoryView = <some toolbar with buttons or any other view>
Happy coding :)
I will suggest you one solution with custom View.
datePickerContainer is UIView:
//datepicker
datePickerContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 440, 310, 300)];
datePickerContainer.opaque = YES;
datePickerContainer.backgroundColor = [UIColor clearColor];
UIImageView *pickerNavBar = [[UIImageView alloc] init];
UIImage *barImage = [UIImage imageNamed:#"pickerbar.png"];
pickerNavBar.image = barImage;
pickerNavBar.frame = CGRectMake(0, 7, barImage.size.width, barImage.size.height);
[datePickerContainer addSubview:pickerNavBar];
[pickerNavBar release];
UIButton* blueButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[blueButton setTitle:#"Done" forState: UIControlStateNormal];
blueButton.frame = CGRectMake(255, 15, 55, 30);
[blueButton addTarget:self action:#selector(hidePickerViewContainer) forControlEvents:UIControlEventTouchUpInside];
[datePickerContainer addSubview:blueButton];
[blueButton release];
//UIPickerView Initialization code
UIPickerView *pickerViewCountry = [[UIPickerView alloc] init];
pickerViewCountry.showsSelectionIndicator = YES;
pickerViewCountry.dataSource = self;
pickerViewCountry.delegate = self;
pickerViewCountry.frame = CGRectMake(0,35 , 320, 15);
[datePickerContainer addSubview:pickerViewCountry];
[pickerViewCountry release];
//set container and release
[self.view addSubview: datePickerContainer];
[datePickerContainer release];
To show datePickerContainer UIVie use this code:
-(void)showPickerViewContainer{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
CGRect frameContainer = datePickerContainer.frame;
frameContainer.origin.y = 150.0f;
datePickerContainer.frame = frameContainer;
[UIView commitAnimations];
}
To handle done button and hide pickerView use this:
-(void)hidePickerViewContainer{
//here use you custom code to handle done action
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
CGRect frameContainer = datePickerContainer.frame;
frameContainer.origin.y = 440.0f;
datePickerContainer.frame = frameContainer;
[UIView commitAnimations];
}
This is just a custom solution. Hope this help.
declare variable in header file
UIActionSheet *actionSheet;
NSString *pickerType;
Write Below code into your implementation file:
- (void)createActionSheet {
if (actionSheet == nil) {
// setup actionsheet to contain the UIPicker
actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone:)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
[actionSheet addSubview:pickerToolbar];
[pickerToolbar release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];
}
}
-(IBAction)BtnPressed:(id)sender
{
[self createActionSheet];
pickerType = #"picker";
select = NO;
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
chPicker.dataSource = self;
chPicker.delegate = self;
chPicker.showsSelectionIndicator = YES;
[actionSheet addSubview:chPicker];
sessoTxt.text = [sessoArray objectAtIndex:0];
[chPicker release];
}
declare delegate methods
#pragma mark UIPickerViewDelegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
int count;
if ([pickerType isEqualToString:#"picker"])
count = [array count];
return count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *string;
if ([pickerType isEqualToString:#"picker"])
string = [array objectAtIndex:row];
return string;
}
// Set the width of the component inside the picker
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 300;
}
// Item picked
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
select = YES;
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:row];
}
}
- (void)pickerDone:(id)sender
{
if(select == NO)
{
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:0];
}
}
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
[actionSheet release];
actionSheet = nil;
}
}
I try to present a modal view (InfoViewController) from a navigation view controller(DateViewController).
I add a toolbar on top of InfoViewContoller's view. Now I want to add a title "Info" and a "Done" button on the toolbar.(The Done button will perform the infoDismissAction method)
Can anyone give me som tips? Thanks a lot!
Here's code of DateViewController.h
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
#interface DateViewController : UIViewController
{
InfoViewController *infoViewController;
}
#property (nonatomic, retain) InfoViewController *infoViewController;
#end
DateViewController.m
- (IBAction)modalViewAction:(id)sender{
if (self.infoViewController == nil)
self.infoViewController = [[[InfoViewController alloc] initWithNibName:
NSStringFromClass([InfoViewController class]) bundle:nil] autorelease];
[self presentModalViewController:self.infoViewController animated:YES];
}
- (void)dealloc{
if (self.infoViewController != nil)
{
[infoViewController release];
}
[super dealloc];
}
- (void)viewDidLoad{
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Info" style:UIBarButtonSystemItemPlay target:self action:#selector(modalViewAction:)] autorelease];
[modalBarButtonItem release];
[super viewDidLoad];
}
Here's InfoViewController.m
- (IBAction)infoDismissAction:(id)sender{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
[self.view addSubview:toolBar];
[toolBar release];
[backButton release];
[super viewDidLoad];
}
Try this code.
- (void)viewDidLoad {
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *infoButton = [[[UIBarButtonItem alloc] initWithTitle:#"INFO" style:UIBarButtonItemStyleBordered target:self action:#selector(InfoAction:)] autorelease];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"DONE" style:UIBarButtonItemStyleBordered target:self action:#selector(doneAction:)];
NSArray *barButton = [[NSArray alloc] initWithObjects:flexibleSpace,infoButton,flexibleSpace,doneButton,nil];
[toolBar setItems:barButton];
[self.view addSubview:toolBar];
[toolBar release];
[barButton release];
barButton = nil;
[super viewDidLoad];
}
I'd recommend setting up another UINavigationController with your InfoViewController and present the navigation controller as your modal view.
To answer your question you'd want to fill in your UIToolbar like this:
- (void)viewDidLoad {
[super viewDidLoad];
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
[self.view addSubview:toolBar];
[toolBar release];
UIBarButtonItem* bbiInfo = [[UIBarButtonItem alloc] initWithTitle:#"Info" style:UIBarButtonItemStyleBordered target:self action:#selector(tappedInfoButton)];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* bbiDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(tappedDoneButton)];
NSArray* items = [[NSArray alloc] initWithObjects:bbiInfo, flexibleSpace, bbiDone, nil];
[toolBar setItems:items];
[items release];
[bbiInfo release];
[flexibleSpace release];
[bbiDone release];
}