1

我这里有一个情况,请帮帮我,

1)我有一个带有自定义单元格的表格 2)每个单元格都有 2 个搜索栏和 2 个标签。

我正在尝试的是假设用户开始编辑搜索栏,弹出框应该指向该搜索栏。

我已经实现了这一点,但弹出框没有出现在所需的搜索栏上,而且弹出框的高度有时也太长了

if (searchBar.tag==10) {
    NSLog(@"Display date popover");
    CGRect pickerFrame = CGRectMake(0,0,300,200);

    UIViewController *tempDateViewController=[[UIViewController alloc] init];

    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];

    [datePicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];

    [tempDateViewController.view addSubview:datePicker];

    if(!currentPopover)
    {

        currentPopover=[[UIPopoverController alloc] initWithContentViewController:tempDateViewController];
    }
    else {

        [currentPopover setContentViewController:tempDateViewController animated:YES];

    }

    tempDateViewController.contentSizeForViewInPopover=CGSizeMake(320, 300);
    [datePicker release];
    [tempDateViewController release];
    [currentPopover presentPopoverFromRect:searchBar.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];



}   

请帮我解决这个问题。提前谢谢。

4

1 回答 1

3

searchBar 位于表格视图中的单元格内(根据您的布局,它可能位于其他视图中)。问题很可能是 self.view 不是您调用它的 searchBar 的直接父级。所以在 self.view 中使用 searchBar 的框架会得到意想不到的结果。

而不是使用 self.view 并试图找出 searchBar 相对于它的位置,您可以将 searchBar 本身用于“inView”和“rect”,使用 searchBar 的边界:

[currentPopover presentPopoverFromRect:searchBar.bounds inView:searchBar 
    permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];


接下来,要修复弹出框的高度,请尝试设置弹出框的 popoverContentSize而不是视图控制器的 contentSizeForViewInPopover:

//tempDateViewController.contentSizeForViewInPopover=CGSizeMake(320, 300);
currentPopover.popoverContentSize = CGSizeMake(320, 300);


最后,一个单独的问题,但日期选择器的最小高度应该是 216,而不是 200:

CGRect pickerFrame = CGRectMake(0,0,300,216);
于 2011-03-10T13:37:06.213 回答