0

我在 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

在此处输入图像描述

4

1 回答 1

1

更改此行

datePicker.frame=CGRectMake(0,44,320, 216);

到这条线

datePicker.frame = CGRectMake(0, 0, 320, 216);

然后在你的视图控制器上设置 contentSizeForViewInPopover,像这样

popoverContent.contentSizeForViewInPopover = CGSizeMake(320.0, 216.0);
于 2013-04-02T01:04:43.313 回答