6

私は UITabBarController を持っており、各タブは必要に応じてスタックの新しいコントローラーをプッシュする異なる UIViewController を処理します。私が必要とするこれらのタブのうちの 2 つは、特定のコントローラーに到達したときに、iPhone を回転させ、横向きモードでビューを視覚化する機能です。多くの苦労の末、UITabBarController をサブクラス化して shouldAutorotateToInterfaceOrientation をオーバーライドすることが必須であることがわかりました。ただし、実装で単純に YES を返すと、次の望ましくない副作用が発生します。

iPhoneを回転させると、すべてのタブのすべてのコントローラーが自動的に横向きモードになります。

各コントローラーで shouldAutorotateToInterfaceOrientation をオーバーライドして NO を返すようにしても機能しません。iPhone を回転させると、コントローラーは横向きモードになります。

サブクラス化された UITabBarController で次のように shouldAutorotateToInterfaceOrientation を実装しました。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if([self selectedIndex] == 0 || [self selectedIndex] == 3)
        return YES;

    return NO;
}

私が興味を持っている 2 つのタブだけが実際に横向きモードをサポートするようにします。特定のタブのスタックにある特定のコントローラーでランドスケープ モードをサポートする方法はありますか?

私は成功せずに、次のようなことを試みました

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

if([self selectedIndex] == 0 || [self selectedIndex] == 3)
{   
   if ([[self selectedViewController] isKindOfClass: [landscapeModeViewController class]])
           return YES;
    }

     return NO;

}

また、デリゲート メソッド didSelectViewController を使用してみましたが、成功しませんでした。どんな助けでも大歓迎です。ありがとうございました。

4

7 に答える 7

4

これは私のために働いた:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if(self.selectedIndex == 0 && [[[self.viewControllers objectAtIndex:0] visibleViewController] isKindOfClass:[MyViewController class]])
        return YES;
    else
        return NO;
}
于 2009-04-21T13:34:26.287 に答える
3

これをしばらくの間(アプリのタブバーコントローラーから)問題なく使用できました:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

こうすることで、適切な VC で実際のチェックを行うことができます。この場合は、フォト ギャラリー ビュー (他に何がありますか?) を確認します。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

私のギャラリー ビューは、特定の Nav コントローラーのスタックの一番上にさえありません。それでも呼び出されます。

残念ながら、VC が(4 つのメイン タブではなく) MoreViewController内に潜んでいる場合、これがうまく機能しないことがわかりました。その場合、ギャラリー VC が呼び出されることはありません。私がずっと呼び出してきた VC は、実際には選択したタブのナビゲーション コントローラーであり、それが適切な VC (この場合は私のフォト ギャラリー VC) に情報を伝達するためだと思います。しかし、More VC の場合、物事はうまく機能しません... ああ、そこから順番に下り坂になります。:\

Andreas による変更 (このスレッドの他の場所を参照) を使用してみましたが、役に立ちませんでした。手がかり歓迎!

于 2009-08-12T23:20:37.337 に答える
2

UITabBarController を操作したときと同じ問題に遭遇しました。どの UIViewController を回転できるようにし、どの UIViewController を回転できないようにするかを制御する必要がありました。私の主な問題は、[MORE] タブにありました。MORE タブに含まれる UIViewController を回転させたくありませんでした。

私の解決策は、MyTabBarController と呼ばれる独自の UITabBarController を作成することでした。

@interface MyTabBarController : UITabBarController <UITabBarDelegate> {

}

次に、shouldAutorotateToInterfaceOrientation メソッドを実装しました。

@implementation MyTabBarController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 UIViewController *controller = [self selectedViewController];

 if ((controller == [self moreNavigationController]) || ([self selectedIndex] == 4))
 {
  return interfaceOrientation == UIInterfaceOrientationPortrait;
 }

 return [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

@end

[MORE] タブが選択されているかどうかを確認する必要がありました。これは 2 段階のプロセスです。MORE タブが最初に選択されると、API は 4 より大きい selectedIndex を返すため、選択したコントローラーを moreNavigationController と比較する必要がありました。

MORE タブ内から UIViewController が選択された場合、selectedIndex は最終的に 4 になりますが、selectedController は moreNavigationController ではなく、選択された UIViewController になります。

if ( (controller == [self moreNavigationController]) || ([self selectedIndex] == 4))がこの問題を処理します。

今、アプリケーションを実行すると、[MORE] タブの UIViewControllers が回転しません。これが、私と同じ問題に直面している他の開発者に役立つことを願っています。

エミリオ

于 2010-01-09T20:11:31.270 に答える
1

ここや他の場所で見たものshouldAutorotateから、古い方法shouldAutorotateToInterfaceOrientationは廃止されたため、この方法を使用するソリューションをつなぎ合わせました。

UITabBarController のカテゴリ内に配置しました。これが許容されることを願っています!

// call to method shouldAutorotate replaces call to method shouldAutorotateToInterfaceOrientation (deprecated)
-(BOOL)shouldAutorotate
{ // check whether selected view controller should autorotate    
  UIViewController *controller = self.selectedViewController;
  if ([controller isKindOfClass:[UINavigationController class]])
    { // in case it is a navigation controller: get visible view of that
      controller = [(UINavigationController *)controller visibleViewController];
    }
  return [controller shouldAutorotate];
}
于 2013-04-04T09:40:48.937 に答える
0

(上記の受け入れられた回答で提案されているように) UITabBarController をサブクラス化しても本当に問題ありませんか?

Apples が「UITabBarController または UINavigationController をサブクラス化してはならない」のようなことを言っていることを理解しました-または誤解しましたか?

ともかく; UITabBarController を配置する UIViewController をサブクラス化するこのチュートリアルを見つけました。

于 2011-02-18T07:35:56.633 に答える
0

ありがとう、ありがとう、ありがとう。これを行う方法を理解するのに2日かかりました。これは、navigationControllers を備えた tabBarController がある場合のすべての大きな助けに対する私の見解です。

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

UIViewController *controller = self.selectedViewController;
if ([controller isKindOfClass:[UINavigationController class]])
    controller = [(UINavigationController *)controller visibleViewController];

if([controller isKindOfClass:[LOCviewcontroller class]])
    return YES;
else
    if([controller isKindOfClass:[personWebSiteView class]])
        return YES;
else return NO; 

}

初心者のコーダーのコードに対する批判はいつでも大歓迎です...ジャック

于 2010-08-04T23:37:07.033 に答える