我在UIView中有一个看起来像这样的UIDatePicker(我将背景颜色更改为红色,因此它可以突出这个问题):
我希望UIView适合UIDatePicker,所以没有红色。这是我的代码:
-(void) showModalCalendar:(int)tag {
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
popoverView.backgroundColor = [UIColor redColor];
// the full UIDatePicker
datePicker=[[UIDatePicker alloc]init];
datePicker.frame=CGRectMake(0,44,320, 216);
// set the locale to en-GB for testing!
NSLocale *uk = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
[datePicker setLocale:uk];
if(tag == 202) {
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker setTag:205];
}
else if(tag == 203) {
datePicker.datePickerMode = UIDatePickerModeTime;
[datePicker setTag:206];
}
[datePicker setMinuteInterval:15];
[datePicker addTarget:self action:@selector(dateDidChange:) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate = (id)self;
[popoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
switch (tag) { // displays the popover datepicker
case 202: // appointment start time
[popoverController presentPopoverFromRect:boApptStart.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
break;
case 203: // appointment end time
[popoverController presentPopoverFromRect:boApptEnd.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
break;
}
}
我需要做什么或改变什么?
更新 - 我们越来越近了! (我认为):D
答案 0 :(得分:1)
更改此行
datePicker.frame=CGRectMake(0,44,320, 216);
到这一行
datePicker.frame = CGRectMake(0, 0, 320, 216);
然后在视图控制器上设置contentSizeForViewInPopover,就像这样
popoverContent.contentSizeForViewInPopover = CGSizeMake(320.0, 216.0);