0

私は初心者なので、これがばかげた疑いである場合はご容赦ください...最近プロジェクトをxcode3からxcode4に変更しましたが、いくつかの問題があり、理由がわかりません...

スクロールビューのフレームとコンテンツ サイズ、および向きが変更された場合の変更を管理するには、次のようにします。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

 if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||

      toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)     {

      scrollView.frame = CGRectMake(0,0,480,300);

      [scrollView setContentSize:CGSizeMake(480,560)];

 }

 else {

      scrollView.frame = CGRectMake(0,0,320,460);

      [scrollView setContentSize:CGSizeMake(320,560)];

 }

 [scrollView flashScrollIndicators];}

Xcode3では画面読み込み時にアプリがこのメソッドを呼び出すので、どのような状況でも問題なく動作していたと思います。

しかし、Xcode4 では、アプリは画面の読み込み時にこのメソッドを呼び出しません。向きが変わったときにのみ呼び出されます(この違いが存在する理由はわかりません)。そのため、画面が読み込まれるとスクロールビューが機能しません。デバイスの向きを変えると動き始めます。

次に、これを試しました:

- (void)viewDidLoad {

if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft ||

      [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)     {

      scrollView.frame = CGRectMake(0,0,480,300);

      [scrollView setContentSize:CGSizeMake(480,560)];

 }

 else {

      scrollView.frame = CGRectMake(0,0,320,460);

      [scrollView setContentSize:CGSizeMake(320,560)];

 }

[scrollView flashScrollIndicators];

 [super viewDidLoad];}

どうやら機能していたようですが...非常に奇妙なことが起こります...

別のビュー (縦向き) に移動し、向きを横向きに変更してから最初のビュー (横向き) に戻ると、ビューの自動サイズ変更マスクが狂ってしまいます。ビューの幅が実際よりも広いかのように、すべてのコンテンツが右側に移動します。また、コンテンツの一部にアクセスできません。しかし、向きを縦向きに変更すると、もう一度横向きにしても問題ありません。したがって、横方向の別のビューからビューに来るときだけ間違っています。

これは、ビューから別のビューに渡す方法です。

- (IBAction) btnUnVano:(id) sender {


 PE2100UnVano *controller = [[PE2100UnVano alloc] initWithNibName:@"PE2100UnVano" bundle:nil];

 controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

 [self presentModalViewController:controller animated:YES];

 [controller release];}

私は何をすべきか?

Pd: 私の英語でごめんなさい


編集

OK、この部分は修正されました。willAnimateRotationToInterfaceOrientation を削除しましたが、これで正しく動作するようになりました:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Si la orientación es horizontal..
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft ||
    [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)   {
    scrollView.frame = CGRectMake(0,0,480,300);
    [scrollView setContentSize:CGSizeMake(480,560)];
}
// Si la orientación es vertical..
else {
    scrollView.frame = CGRectMake(0,0,320,460);
    [scrollView setContentSize:CGSizeMake(320,560)];
}
// Mostrar durante un instante los indicadores de scroll
[scrollView flashScrollIndicators];
// Retorna YES para soportar todas las orientaciones posibles
return YES;}

さて、別の問題があります。一部の画面では、次のコードで前の画面に戻ります。

- (IBAction) btnAtrasTabla:(id) sender {
[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];}

この場合、オートサイズの問題が再び発生します。ポートレートでこの画面に移動し、ランドスケープに変更し、前の画面に戻ると、前の画面が間違ったオートサイズで表示されます。次に、方向を変更すると、すべてが再び正しくなります... 何かアイデアはありますか?

4

2 に答える 2

0

rootViewControllerに追加されたviewController.viewは、rootViewController自体からその動作を継承します。

[self.view addSubview:anotherViewController.view];

これは、rootViewControllerでのみshouldAutorotateToInterfaceOrientationを有効にする必要があることを意味します。

viewControllerをモーダルビューとして提示すると、さまざまな動作が発生します。

[self presentModalViewController:anotherViewController animated:YES];

したがって、親とモーダルビュー自体でshouldAutorotateToInterfaceOrientationを有効にする必要があります。

正しいアーキテクチャで問題を解決できるはずですが、コードをクリーンアップできない場合は回避策があると思います。現在のビューの向きを確認し、他のビューに通知を送信して、フレームを設定します。この回避策は役立つ可能性がありますが、お勧めしません。

于 2012-01-31T16:43:45.397 に答える
0

Beppeのアドバイスを受けて、私が最終的にやったことがここにあります.

「子」ビューで、離れると通知を送信します。

[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"resize" object:nil userInfo:nil];

次に、「親」ビューはオブザーバーです。

- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shouldAutorotateToInterfaceOrientation:) name:@"resize" object:nil];
[super viewDidLoad];}

これを行うと、「子」ビューから「親」ビューに戻るときに、 shouldAutorotateToInterfaceOrientation メソッドが呼び出されます。この方法では、アプリの現在の向きに合わせてすべてのサイズを変更していることを思い出してください。

于 2012-02-01T21:14:16.933 に答える