2017-07-10 4 views
1

Objective-Cの新機能です。私は特定のビューのためにポートレートモードを修正したい。風景モードを使用している他のビューとビューを終了するときにポートレートモードを戻す。しかし、まだ特定のビューで横長モード。Objective-Cの特定のビューのポートレートモードを修正する方法はありますか?

- (NSUInteger) supportedInterfaceOrientations { 
// Return a bitmask of supported orientations. If you need more, 
// use bitwise or (see the commented return). 
return UIInterfaceOrientationMaskPortrait; 
// return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;} 

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation { 
// Return the orientation you'd prefer - this is what it launches to. The 
// user can still rotate. You don't have to implement this method, in which 
// case it launches in the current orientation 
return UIInterfaceOrientationPortrait; } 

特定のビューにコードを追加します。

[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"]; 

そして、このコードはこのメソッドを入力します。

これを修正するにはどうすればよいですか?

答えて

1

本当にとても簡単です。 'ViewController'と 'SecondViewController'の2つのViewControllerクラスを用意しましょう

まず、 'AppDelegate'クラスでSecondViewControllerをインポートする必要があります。

は、その後、あなたのAppDelegate.mファイル今

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

    if ([[self visibleViewController:[UIApplication sharedApplication].keyWindow.rootViewController] isKindOfClass:[SecondViewController class]]) { 
     return UIInterfaceOrientationMaskPortrait; 

    } 
    return UIInterfaceOrientationMaskLandscape; 
} 

- (UIViewController *)visibleViewController:(UIViewController *)rootViewController { 

    if (rootViewController.presentedViewController == nil) { 

     return rootViewController; 

    } 

    if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]) { 

     UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController; 
     UIViewController *lastViewController = [[navigationController viewControllers] lastObject]; 

     return [self visibleViewController:lastViewController]; 

    } 

    if ([rootViewController.presentedViewController isKindOfClass:[UITabBarController class]]) { 

     UITabBarController *tabBarController = (UITabBarController *)rootViewController.presentedViewController; 
     UIViewController *selectedViewController = tabBarController.selectedViewController; 

     return [self visibleViewController:selectedViewController]; 
    } 

    UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController; 

    return [self visibleViewController:presentedViewController]; 
} 

実行し、参照して、この2つのメソッドをコピーして貼り付けます。

+1

ありがとうございました!あなたのコードそれは私が欲しいものです。さらに私はこの問題を解決しました。つまり、私はこのラインの風景モードをコード化しています - > [[UIDevice currentDevice] setvalue:~~]だから、私はコードの風景 - >肖像画を変更します。 –

+0

私はあなたを助けてとてもうれしいです。 –

関連する問題