UIDatePicker自定义

时间:2013-04-29 07:44:41

标签: ios uialertview uidatepicker uidatepickermodetime

我已将UIDatePicker定制到某个级别,我将其添加到UIAlertView。

enter image description here

但我在日期选择器的底部和顶部有一个黑色,如何删除它。我希望日期选择器整洁干净。

我用来实现这个目的的代码:

UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.frame=CGRectMake(20, 45.0, 240.0, 150.0);
datePicker.minimumDate=[NSDate date];

//Forget about this logic.

NSString *alertTitleString=@"";
if(self.presentingViewController !=nil)
{
    alertTitleString=@"Select Date and Time";
    [datePicker setDatePickerMode:UIDatePickerModeDateAndTime];
}
else
{
    alertTitleString=@"Select Time";
    [datePicker setDatePickerMode:UIDatePickerModeTime];

}
[datePicker setDate:[NSDate date]];

UIView *view = [[datePicker subviews] objectAtIndex:0];
[view setBackgroundColor:[UIColor clearColor]]; // hide the first and the last subviews
[[[view subviews] objectAtIndex:0] setHidden:YES];
[[[view subviews] lastObject] setHidden:YES];



 UIAlertView *eventStartDateAlert = [[UIAlertView alloc] initWithTitle:alertTitleString message:@"\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"Ok") otherButtonTitles:nil];
eventStartDateAlert.delegate = self;
[eventStartDateAlert addSubview:datePicker];
[eventStartDateAlert show];

1 个答案:

答案 0 :(得分:1)

基本上你要做的是删除顶部和底部的渐变,对吧?

这种方法很有用,至少对于iOS 5和6来说。

[view.subviews[5]  setHidden:YES];
[view.subviews[11] setHidden:YES];
[view.subviews[17] setHidden:YES];