私のアプリには 3 つの UIPopOvers があります。ユーザーがツールバーのボタンをタップすると表示されます。ポップオーバーが既に開いている場合 (-willAnimateRotationToInterfaceOrientation: のように)、ユーザーが iPad を回転させたときに、ポップオーバーが正しい場所に表示されるようにする必要があります。
どうすればいいですか?
前もって感謝します!
私のアプリには 3 つの UIPopOvers があります。ユーザーがツールバーのボタンをタップすると表示されます。ポップオーバーが既に開いている場合 (-willAnimateRotationToInterfaceOrientation: のように)、ユーザーが iPad を回転させたときに、ポップオーバーが正しい場所に表示されるようにする必要があります。
どうすればいいですか?
前もって感謝します!
iOS 7.0 以降では、UIPopoverControllerDelegateで利用可能な次のメソッドを実装することで実行できます。
(void)popoverController:(UIPopoverController *)popoverController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView **)view
presentPopoverFromRectメソッドを使用して提示されたポップオーバーの場合、ポップオーバー コントローラーは、インターフェイスの向きが変更されたときにこのメソッドを呼び出します。
これまでに見つけた唯一の解決策は、デバイスが回転したときにポップオーバーを閉じることです/
これは、私のプロジェクトの 1 つからのコード フラグメントです。基本的に、ポップオーバーが表示されている場合は、メソッドでポップオーバーを再度提示します。これは、ユーザー インターフェイスの回転が行われた後didRotateFromInterfaceOrientation:
にビュー コントローラーに送信されます。(およびメソッドはローテーションが行われる前に呼び出されるため、メソッド呼び出しには不適切な場所です。)willRotate...
willAnimateRotation...
presentPopover...
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// if the popover is showing, adjust its position after the re-orientation by presenting it again:
if (self.myPopoverController != nil) // if the popover is showing (replace with your own test if you wish)
{
[self.myPopoverController presentPopoverFromRect:attachmentRect
inView:myView
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
}
上記でself.myPopoverController
は、作成時にポップオーバーへの参照を保存するビューコントローラーのプロパティです。通常の状況でポップオーバーを閉じて破棄するときは、このプロパティを に設定するように注意します。これにより、ポップオーバーが表示されているかどうかを判断するnil
ために「非」であることを確認できます。nil
ただし、回転が行われる前にポップオーバーを閉じる必要はないことに注意してください。同じポップオーバーをもう一度提示するだけです。(ここで、ポップオーバーへの参照を維持すると便利です。)
あなたの場合、ポップオーバーがツールバー ボタンから生成される場合、代わりに次のようなものを使用します。
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// if the popover is showing, adjust its position after the re-orientation by presenting it again:
if (self.myPopoverController != nil) // if the popover is showing (replace with your own test if you wish)
{
[self.myPopoverController presentPopoverFromBarButtonItem:barButtonItem
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
presentPopoverFromBarButtonItem メソッドを使用してポップオーバーを表示するだけの場合、デバイスが回転すると、ポップオーバーは新しいボタン位置の正しい位置に自動的に移動します。
私はこの同じ問題に数回遭遇しました。私は通常、次のようにポップオーバーを中央に表示する方法を作成します。
- (void) showPopoverForSize:(CGSize) size center:(CGPoint) center {
CGFloat width = size.width;
CGFloat height = size.height;
CGFloat x = center.x - width / 2;
CGFloat y = center.y - height / 2;
CGRect frame = CGRectMake(x, y, width, height);
popover.popoverContentSize = frame.size;
[popover presentPopoverFromRect:frame inView:self.view permittedArrowDirections:0 animated:YES];
}
次に、didRotate で次のようにします。
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
if (popover.isPopoverVisible)
[self showPopoverForSize:popover.popoverContentSize center:self.view.center];
}
これにより、どの向きでもポップオーバーが中央に配置されます。
presentPopoverFromBarButtonItem または FromRect を呼び出していますか? 回転時に BarButtonItem に変更を加えていますか?
Apple のドキュメントには、FromRect の回転時に位置を管理する必要がある、またはバー ボタン項目を変更する必要があると具体的に記載されています。http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPopoverController_class/Reference/Reference.htmlの 4 番目の段落を参照してください。