2010-10-04 71 views
4

當我在iPad中縱向切換到橫向視圖(& Vice Versa)時,彈出窗口視圖的位置出現亂碼。下面是我如何計算我的popover視圖的框架:彈出視圖的位置問題

aRect = self.myElement.frame; 
aRect.origin.x += aRect.size.width; 

[aPopOver presentPopoverFromRect:aRect inView:self.myElement.superview permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES]; 

請告訴我這裏有什麼問題嗎?

回答

1

好吧,我注意到你的代碼有點奇怪。

您是否將寬度的大小添加到aRect的x位置的原點? aRect.origin.x + = aRect.size.width;

IM假設你想這是右上角....

你可以取消你的.m文件中的代碼,使它像這樣:

// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    Return YES; // for supported orientations 
    //otherwise return (interfaceOrientation == UIInterfaceOrientationLandscape); if you want only landscape mode. 
} 

或者什麼,我會如果你想佈置你的子視圖在您的情況做的是使用didRotateFromIntferfaceOrientation:像這樣:

(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    [self layoutSubviews]; 
} 

也layoutSubviews

- (void)layoutSubviews 
{ 
    NSLog(@"layoutSubviews called"); 

    ...recalc rects etc based on the new self.view.bounds... 
} 

它的工作原理是這樣的。

PK

+0

是的,我正在嘗試這種方式在彈出視圖中間的右側對齊。通過在原點中添加寬度,我可以在啓動應用程序時將它放置在正確的位置。問題出現在我改變方向時。 – Abhinav 2010-10-04 23:24:09

+0

所以你應該嘗試的是didRotateFromInterfaceOrientation我會更新我的文章,你可以這樣做。 – Pavan 2010-10-04 23:25:12

+0

只是一個問題,你有一個標籤欄在你的應用程序? – Pavan 2010-10-04 23:35:17

5

UIPopoverController documentation:(重點煤礦)

如果用戶同時 酥料餅是可見的旋轉設備中,酥料餅 控制器隱藏酥料餅,然後 示出了它再次在 輪換結束。彈出窗口控制器 嘗試適當地爲您放置彈出窗口 ,但您可能有 在某些情況下再次顯示它或將其隱藏 。例如,從一個欄按鈕項顯示時 , 的酥料餅的控制器自動 調整位置(和潛在 大小)的酥料餅的佔 更改到杆 按鈕項的位置。但是,如果你 旋轉過程中去除 欄按鈕項目,或者如果從目標矩形的 視圖提出的 酥料餅中,酥料餅的控制器不 試圖重新定位酥料餅。在 這些情況下,您必須手動隱藏 彈出窗口或將其從 再次呈現爲適當的新位置。您可以使用 在 didRotateFromInterfaceOrientation: 中執行此操作,您可以使用 用於呈現彈出窗口的視圖控制器的方法。

+0

好的。但是,我應該在'didRotateFromInterfaceOrientation'方法中寫入哪些代碼。我彈出的顯示邏輯與控制器方法緊密結合,而且,我的視圖由自定義UI小部件組成。在一個這樣的UI小部件中,我展示了這個彈出視圖。 – Abhinav 2010-10-04 23:45:18

1

這是一個古老的問題,但我看到它不清楚OP應該如何處理Kris Markel的建議。這在Technical Q&A QA1694中有記錄。只是再次呈現popover。

比方說,你已經把你的popover代碼放在一個名爲showPopover的方法中。只需在旋轉時調用該方法,首先確保它是可見的:

-(void)showPopover { 
    aRect = self.myElement.frame; 
    aRect.origin.x += aRect.size.width; 

    [self.aPopOver presentPopoverFromRect:aRect inView:self.myElement.superview permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES]; 
} 

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)oldOrientation 
{ 
    if ([self.aPopover isPopoverVisible]) { 
     [self showPopover]; 
    } 
}