10

ビューの1つにランドスケープを強制する次の方法を試しました:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeLeft;
}

- (BOOL)shouldAutorotate{
    return YES;
}

どちらもうまくいきませんでした。シミュレーターと iPad でテストしていることに注意してください。

ありがとう

4

3 に答える 3

22

これは、NavigationViewController を使用してビューの 1 つを強制的にランドスケープにする方法です。

  1. この回答を実装しました: https://stackoverflow.com/a/12662433/2394787

  2. View コントローラーにインポートされたメッセージ: objc/message.h

  3. viewDidLoad メソッドに次のコード行を追加しました。

objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);

それが誰かを助けることを願っています。

于 2013-10-01T11:11:41.093 に答える
1
int shouldShowInLandscape = 0;

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(forceToLandscape:)
                                                 name:@"yourNameNotification"
                                               object:nil];

}

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
              return UIInterfaceOrientationMaskLandscapeLeft;
    } else {
            if(shouldShowInLandscape)
            {
                return UIInterfaceOrientationMaskLandscape;
            }
            return UIInterfaceOrientationMaskPortrait;
    }
}

-(void) forceToLandscape:(NSNotification*) theNot
{
    shouldShowInLandscape = [[theNot object] intValue];

}

// UIViewController から

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"yourNameNotification"
     object:[NSString stringWithFormat:@"1"]];
}

-(void) viewDidDisappear:(BOOL)animated
{

        [[NSNotificationCenter defaultCenter]
     postNotificationName:@"yourNameNotification"
     object:[NSString stringWithFormat:@"0"]]; 

}

// notificationCenter を削除します

于 2014-08-09T00:05:41.013 に答える