iPhone:DatePicker dd/mm/yyyy - iphone

I am using a DatePicker with popups when a user click a textField. However, the Picker displays Month first, not date first and it isn't British way to write a date.
Is there any way to set the date format in DatePicker to British way such as dd/mm/yyyy?
I am using an Achtionsheet:
-(IBAction)acsheet:(id)sender
{
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
//CGRect pickerFrame = CGRectMake(0, 45, 0, 0);
pickerView1 = [[UIDatePicker alloc] init];
pickerView1.datePickerMode = UIDatePickerModeDate;
[actionSheet addSubview:pickerView1];
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 blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:)
forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
[pickerView1 addTarget:self
action:#selector(updateLabel:)
forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissDatePicker:)] ;
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleBlackTranslucent;
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:toolBar];
}

The UIDatePicker, by default, uses whatever [NSLocale currentLocale] dictates.
The locale property on UIDatePicker was deprecated in iOS 5, but I believe that you could do the following:
NSCalendar *cal = [NSCalendar currentCalendar];
cal.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
myDatePicker.calendar = cal;
This should work. If it doesn't, please file a bug and I'll fix it. :)

The UIDatePicker is made to adapt to the user's settings. Changing its format using the locale: method was possible but is now deprecated in iOS 5.0.
You can view your format in the settings of your iPhone :
Settings > General > International > Region Format
If you want a specific format, consider making your own picker but it is very discouraged for a date picker.

When writing the month in text the british probably would put the month first (or at least it would be acceptable).
But to answer your question I'm not aware of a way to change the display format of the date picker, but you could make you're own using a UIPickerView with a custom delegate and data source.

Related

Adding UIPickerView to UIActionSheet (buttons at the bottom)

I want to
show a picker view (sliding up)
deactivate the background while it's visible
show (UIActionSheet) buttons at the bottom (not at the top)
It seems to me an easy solution at first since you can find code for adding a picker view to an action sheet everywhere in the web but all solutions position the buttons at top and I need to put the buttons at the bottom of the action sheet (alignment?).
Is this possible? I guess it is if I look at: Example
Regards
Jeven
EDITED (my solution based on coded dads solution)
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 0, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
UISegmentedControl *backButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Back", nil]] ;
[backButton addTarget:self action:#selector(someFunction:) forControlEvents:UIControlEventValueChanged];
backButton.tintColor = [UIColor colorWithRed:0.10 green:0.20 blue:0.52 alpha:0.5];
backButton.segmentedControlStyle = UISegmentedControlStyleBar;
[backButton addTarget:self action:#selector(btnActionCancelClicked:) forControlEvents:UIControlEventAllEvents];
backButton.frame = CGRectMake(20.0, 220.0, 280.0, 40.0);
UISegmentedControl *acceptButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Accept", nil]] ;
[acceptButton addTarget:self action:#selector(anotherFunction:) forControlEvents:UIControlEventValueChanged];
acceptButton.tintColor = [UIColor colorWithRed:0.10 green:0.20 blue:0.52 alpha:0.5];
acceptButton.segmentedControlStyle = UISegmentedControlStyleBar;
acceptButton.frame = CGRectMake(20.0, 280.0, 280.0, 40.0);
[actionSheet addSubview:pickerView];
[actionSheet addSubview:backButton];
[actionSheet addSubview:acceptButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setFrame:CGRectMake(0,150,320, 400)];
... then just apply iPhone 5/iphone 4 dependent view size constraints
EDITED 2:
Just another hint! Originally I wanted to use the standard Actionsheet, but didn't want to place the pickerview at the bottom. I didn't find a way how to move the buttons. Obviously it s really simple: Add UIView as subView on UIActionSheet Good to know ;)
You have 2 possibilites:
Option1: the big tweak.
ActionSheet shows the buttons with the order defined during init. So place some dummy buttons at the top and Cancel will be the only visible, all other buttons will be hidden by the pickerview. Code (warning-tweak at otherButtonTitles):
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Actionsheet"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"1", #"2", #"3", #"4", nil];
UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.delegate = self;
pickerView.dataSource = self;
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = 35;
pickerView.frame = pickerRect;
Option2: the selfmade
menu = [[UIActionSheet alloc] initWithTitle:#"Actionsheet"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.delegate = self;
pickerView.dataSource = self;
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = 35;
pickerView.frame = pickerRect;
UIButton *startbtn = [UIButton buttonWithType:UIButtonTypeCustom];
startbtn.frame = CGRectMake(80,230, 170, 73);
[startbtn setBackgroundImage:[UIImage imageNamed:#"yourbutton.png"] forState:UIControlStateNormal];
[startbtn addTarget:self action:#selector(pressedbuttonCancel:) forControlEvents:UIControlEventTouchUpInside];
[menu addSubview:pickerView];
[menu addSubview:startbtn];
[menu showInView:self.view];
[menu setFrame:CGRectMake(0,150,320, 350)];
Check that for option2 the button should be on the actionsheet unless the clicks will not be dispatched to the selector method.

How to display DatePicker in popupwindow in iPhone?

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];

Determine if a particular UITextField is selected

I have two UITextFields in one UIViewController. I'm opening a UIDatePicker in both the text fields. I want to update the value of the date in the textfields, but I don't know how to check from which textfield's date picker is open. Can someone please explain how I can determine which text field is selected?
- (IBAction)selectATime:(UIControl *)sender {
[self showActionSheetForTimePicker:sender];
}
- (IBAction)showActionSheetForTimePicker:(id)sender
{
//add the action sheet
actionSheetForiPhone = [[UIActionSheet alloc] initWithTitle:#""
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Add the picker
pickerForiPhone = [[UIDatePicker alloc] init];
pickerForiPhone.datePickerMode = UIDatePickerModeTime;
[actionSheetForiPhone addSubview:pickerForiPhone];
[actionSheetForiPhone showInView:self.view];
[actionSheetForiPhone setBounds:CGRectMake(0, 0, 320, 520)];
[pickerForiPhone setBounds:CGRectMake(0, 0, 320, 340)];
//adding toolbar to the action sheet
UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 45)];
toolbar.barStyle = UIBarStyleBlackOpaque;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
//adding buttons to the toolbar
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(dismissPicker:)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(updateTextField:)];
[barItems addObject:cancelButton];
[barItems addObject:flexSpace];//for adding flexible space between cancel and done button
[barItems addObject:doneButton];
[toolbar setItems:barItems];
[actionSheetForiPhone addSubview:toolbar];
CGRect pickerRect = pickerForiPhone.bounds;
pickerRect.origin.y = -100;
pickerForiPhone.bounds = pickerRect;
[pickerForiPhone release];
[actionSheetForiPhone release];
}
- (void)updateTextField:(id) sender
{
//for updating the value of the textfield
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat: #"hh:mm a"];
NSString *time = [df stringFromDate:self.pickerForiPhone.date];
NSLog(#"Time = %#", time);
//if([element respondsToSelector:#selector(setText:)])
{
if(activeTextField == self.fromTime)
self.fromTime.text = time;
else
self.toTime.text = time;
}
[df release];
//for dismissing the date picker
[self.actionSheetForiPhone dismissWithClickedButtonIndex:1 animated:YES];
}
use tags to identify different textfields.Then u can check if (textfield.tag==something) do something
-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField {
[txtField addTarget:self action:#selector(setPicker:)forControlEvents:UIControlEventEditingDidBegin];
}
-(void)setPicker:(id)sender
{
if ([sender tag] == 1) { //first textField tag
//textField 1
}
else {
//textField 2
}
}

UIDatePicker not Displaying correct time

I am using datePicker to create a timePicker . When i click the textField the picker opens and i have select the time in it, after that selected time will display in that textfield . In my case everything works fine except the time displayed in textfield . It shows the wrong time especially minutes.
This my code for creating picker
timePickerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 260)];
timePickerView.backgroundColor = [UIColor clearColor];
//create toolbar using new
UIToolbar *toolbar1;
toolbar1 = [UIToolbar new];
//toolbar.barStyle = UIBarStyleBlackTranslucent;
toolbar1.tintColor = [UIColor colorWithRed:0.627 green:0.627 blue:0.655 alpha:1.000];
//[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.6];
[toolbar1 sizeToFit];
toolbar1.frame = CGRectMake(0, 0, 320, 50);
//Add buttons
UIBarButtonItem *doneButtone1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:#selector(dismissPicker:)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *items1 = [NSArray arrayWithObjects: flexItem1, doneButtone1, nil];
//release buttons
[doneButtone1 release];
[flexItem1 release];
//add array of buttons to toolbar
[toolbar1 setItems:items1 animated:NO];
[timePickerView addSubview:toolbar1];
[toolbar1 release];
UIDatePicker *timePicker = [[UIDatePicker alloc]initWithFrame:CGRectZero];
timePicker.autoresizingMask=UIViewAutoresizingFlexibleWidth;
timePicker.datePickerMode = UIDatePickerModeTime;
timePicker.frame = CGRectMake(0, 50.0f, 320.0, 216);
timePicker.tag = 222;
[timePicker addTarget:self action:#selector(selectTime:) forControlEvents:UIControlEventValueChanged];
[timePickerView addSubview:timePicker];
[timePicker release];
And this is the code to display the time in textfield
UIDatePicker *picker =(UIDatePicker *)sender;
DLog(#"%#",picker);
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
df1.dateStyle = NSDateFormatterMediumStyle;
//if(picker.tag =222) {
[df1 setDateFormat:#"HH:MM"];
self.timeTF.text = [NSString stringWithFormat:#"%#",[df1 stringFromDate:picker.date]];
//timeTF.text=[df1 stringFromDate:[NSDate date]];
[df1 release];
It shows hour hand correctly but minute shows always 5
Whats error in my code . can anyone plz help me find out.
This line is the problem:
[df1 setDateFormat:#"HH:MM"];
MM is month i.e. May = 5
You should use mm.

UIPopoverController without arrows?

I would like to know to make an UIPopoverController without arrows
In fact I would like to simulate something like this:
See that
There is no arrows
There is a title that is somehow inside of a expanded top border of the UIPopoverController and not inside of it like in the normal UIPopoverController.
I assume this is not really an UIPopoverController object but I would appreciate advices on how can I make the same effect (using CoreGraphics? -> specially the translucent degrade effect of the 3D outstanding border) and/or links to some sources if anyone has done this before.
Thanks in advance.
Ignacio
EDIT:
I am still looking for this stuff and realized that even in third party apps is being used
an example is: twitterrific for iPad as seen in this picture.
Anyone please? Putting the title inside the popovercontroller is just ugly.
The below method works fine for me (include iOS7)
[popoverController presentPopoverFromRect:CGRectMake(0, 0, 20, 20)
inView:self.view
permittedArrowDirections:NULL
animated:YES];
Pass 0 to permittedArrowDirections attribute.
[popoverController presentPopoverFromRect:YOUR_RECT
inView:self.view
permittedArrowDirections:0
animated:YES];
While there is some question about whether Apple will approve apps that create a popover without an arrow, you might want to check out this post regarding arrows and this post regarding modal views.
To create a popover with a title you need to create a separate view like you would make a separate window and then load that view in the popover.
The top border is produced by placing a navigation controller between the popover and the presented view controller.
In other words, the popover presents a navigation controller and the navigation controller's root view controller is set to your view controller. This produces the title bar and allows you to set the title with [self setTitle:#"My Title"] and add navigation buttons.
You can add a title by using a UINavigationController, and adding UIViewControllers to the navigation controller. Set the 'title' attribute of the UIViewController to make the title appear.
Setting the arrow direction to NULL, as some have suggested, can result in unpredictable behavior, since the method uses this variable to figure out how to orient the popup relative to your bar button item or rectangle.
It is better to subclass UIPopoverBackgroundView, and set the various arrow return methods to return 0 for the arrows (iOS5 and up only). See this example for how to subclass this properly:
http://blog.teamtreehouse.com/customizing-the-design-of-uipopovercontroller
Simple implementation example (MyCustomPopoverBGView is the subclass of UIPopoverBackgroundView in this example):
UIViewController *vCtrlr = [[UIViewController alloc] initWithNibName:nil bundle:nil];
vCtrlr.title = #"My Title";
self.navCtrlr = [[UINavigationController alloc] initWithRootViewController:vCtrlr];
self.popCtrlr = [[UIPopoverController alloc] initWithContentViewController:_navCtrlr];
_popCtrlr.popoverBackgroundViewClass = [MyCustomPopoverBGView class];
[_popCtrlr presentPopoverFromRect:CGRectMake(0,
0,
320,
150)
inView:self permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
Just copy & Paste the below code
UIViewController *popovercontroller=[[UIViewController alloc] init];
UIView *popoverView=[[UIView alloc] initWithFrame:CGRectMake(312,390, 400, 344)];
popoverView.backgroundColor=[UIColor whiteColor];
popovercontroller.contentSizeForViewInPopover=CGSizeMake(400, 300);
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 400, 0)];
[pickerView setTintColor:[UIColor blackColor]];
[pickerView addTarget:self action:#selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
NSString *bs ; //= [NSString alloc];
// //NSDate *newDate = [NSData alloc];
bs = CurrentSelectedDate;
if (bs.length >= 1) {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init] ;
// //[dateFormatter setDateStyle:NSDateFormatterLongStyle];
// [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"dd-MMM-yyyy"];
// NSDate *myDate = [dateFormatter dateFromString: txtText.text];
pickerView.date = [dateFormatter dateFromString: CurrentSelectedDate];
}
else
{
pickerView.date = [NSDate date];
}
[popoverView addSubview:pickerView];
// pickerView.date = [dateFormatter dateFromString:txtText.text];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
pickerToolbar.barStyle = UIBarStyleDefault;
pickerToolbar.barTintColor=[UIColor colorWithRed:150.0f/255.0f green:91.0f/255.0f blue:129.0f/255.0f alpha:1.0f];
[pickerToolbar sizeToFit];
self.navigationController.toolbar.barTintColor = [UIColor colorWithRed:150.0f/255.0f green:91.0f/255.0f blue:129.0f/255.0f alpha:1.0f];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
doneBtn.tintColor=[UIColor whiteColor];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonPressed:)];
cancelBtn.tintColor=[UIColor whiteColor];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[popoverView addSubview:pickerToolbar];
popovercontroller.view=popoverView;
pickerViewPopup = [[UIPopoverController alloc] initWithContentViewController:popovercontroller];
[pickerViewPopup presentPopoverFromRect:CGRectMake(312, 212, 400, 344) inView:self.view permittedArrowDirections:0 animated:YES];