2011-07-21 52 views
5

所以我有一個viewUIScrollView一堆按鈕。當用戶按下一個按鈕時,我想讓一個UIPopOverController顯示指向所選按鈕。這種方法很有用,但彈出窗口大小不正確,並指向視圖中的隨機點。這是我的代碼。提前致謝。幫助UIPopOverController大小

-(void)detail:(id)sender{ 
    UIButton *button = sender; 
    NSLog(@"tag = %i", button.tag); 

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
    self.popover = [[UIPopoverController alloc] initWithContentViewController:navController]; 
    self.popover.delegate = self; 
    [self.popover presentPopoverFromRect:button.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
} 

比具有酥料餅的尺寸問題: 在這是酥料餅內的視圖,我有:

self.contentSizeForViewInPopover = scroll.contentSize; 
NSLog(@"%f, %f", scroll.contentSize.height, scroll.contentSize.width); 
NSLog(@"showing: %f, %f", self.contentSizeForViewInPopover.height, self.contentSizeForViewInPopover.width); 

和兩個日誌的匹配。所以我認爲一切都應該正常工作。但事實並非如此。這是一個屏幕截圖。讓我知道你是否需要更多我的代碼。提前致謝。

screen shot of the error

回答

1

對於只有酥料餅的大小:我管理的具有可變heighted的UILabel做到這一點:

UILabel *hinweis; 
hinweis.text = @"..."; 
hinweis.frame = CGRectMake(x,y,width,800); 
[hinweis sizeToFit]; 

而對於箭頭:你有沒有嘗試過不同的inView參數像self.parentViewController.view?

+0

在此期間,我學會了要爲tableView使用contentOffset,它應該是模擬的scrollView。 'CGRect f = [tableView rectForRowAtIndexPath:indexPath]; CGPoint yOffset = [tableView contentOffset]; CGRect frame = CGRectMake(f.origin.x,f.origin.y + 45 - yOffset.y,200,40); [self.poc presentPopoverFromRect:frame ...' –

+0

此外,您需要初始化該UILabel。 –

23

我注意到的第一件事是,你應該使用button.frame不是button.bounds爲矩形。這些UIView特性之間的差:

視圖的幾何形狀由它的幀,bounds和中心 屬性中定義。框架屬性定義其超級視圖的座標系中的視圖的原點和尺寸,通常在佈局過程中使用 以調整視圖的大小或位置。

邊界屬性定義視圖的內部維度,因爲它可以看到它們,並幾乎全部用於自定義繪圖代碼。

您可以使用它的setPopoverContentSize消息設置您popover控制器的高度:

// so something like this ... 
[self.popover setPopoverContentSize:CGSizeMake(320, 460)]; 
// and to bind the popover to your button 
[self.popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
+0

將'presentPopoverFromRect'改爲'frame'使它更好一點。但我認爲問題的部分原因是這些按鈕位於「UIScrollView」中,因此當彈出窗口顯示時,原始按鈕框架不再是框架。此外,我不能設置這樣的popover框架,因爲我想根據彈出窗口內文本的大小來更改 – Andrew

+0

@RedBlueThing:謝謝親愛的工作完美 –

+0

@JaspreetSingh我的榮幸:) – RedBlueThing