2016-07-12 9 views
0

デバイスの回転を正しくしようとしています。私はiPadの8.xの/ 9.xのシミュレータ上でテストしてい1 VCの向きをロックすることができません

  • は、私は4つのVC
    • VC1持っている - 両方の肖像画や風景を
    • VC2 - 縦向きと横向きの両方の
    • VC3 - ポートレートのみ
    • VC4 - ポートレートと風景の両方で

目標:すべての回(アプリの向きを縦向きに固定した場合と同じ)で、VC3表示PortraitViewを持っています。

私はVCで

@implementation RotationAwareNavigationController 

- (UIInterfaceOrientationMask)supportedInterfaceOrientations 
{ 
    UIViewController *top = self.topViewController; 
    return top.supportedInterfaceOrientations; 
} 

-(BOOL)shouldAutorotate { 
    UIViewController *top = self.topViewController; 
    return [top shouldAutorotate]; 
} 

@end 

をしようとした肖像画

- (UIInterfaceOrientationMask)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

である。しかし、それは肖像画の寸法に表示されていないビューを意味する動作しない私は何かが足りないのですか?

私は、iOSでImagePickerControllerを使用したときに、それがPortraitに固定されていると確信しています。私はそれをする方法を知らない。

+0

私はあなたがポートレイトだけをサポートし、同時にデバイスの回転を処理する方法を理解していませんか?サポートのみは、回転がないことを意味します。これらのことの両方が欲しいことをもっと詳しく説明できますか? – keithbhunter

+0

VCをポートレートで表示したい。しかし、デバイスを回転させると、その上に別のビューコントローラが表示されます。私はOrientationChangeNotificationsにリスニングすることでそれを行いました。しかし、私は可能な限りviewWillTransitionToSizeを使いたいと思っていました。それ以外の場合は、OrientationChangeNotificationsにリスニングすることに戻ります。 – GJain

+0

私の質問に編集しました。私がポートレートに修正できる限り、それは私のために働くでしょう。 – GJain

答えて

0

異なるビューコントローラで異なる向きをサポートするには、いくつかのことを行う必要があります。まず、ターゲットの[一般設定]タブで、サポートする向きのチェックボックスをすべてオンにする必要があります。

第2に、presentViewControllerまたはdismissViewControllerをアプリに呼び出すと、UIApplicationDelegateの方法application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMaskが呼び出されます。このメソッドを使用すると、新しいView Controllerが表示または非表示になるたびに特定の方向を制限(または許可)できます。残念ながら、UIInterfaceOrientationMaskを返すだけでは簡単ではありません。画面上に表示されるView Controllerを見つけ、それがサポートする向きを返す必要があります。ここではその一例です:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { 
    guard let window = window, let rootViewController = window.rootViewController else { 
     return UIDevice.currentDevice().userInterfaceIdiom == .Pad ? .All : .AllButUpsideDown // iOS defaults 
    } 

    // Let the view controller being shown decide what orientation it wants to support. This method will be called anytime a new view controller is presented on screen. 
    return findVisibleViewController(rootViewController: rootViewController).supportedInterfaceOrientations() 
} 

/// Searches the view hierarchy recursively and finds the view controller that is currently showing. 
private func findVisibleViewController(rootViewController rootViewController: UIViewController) -> UIViewController { 
    if let presentedViewController = rootViewController.presentedViewController { 
     // Search for a modal view first. 
     return self.findVisibleViewController(rootViewController: presentedViewController) 
    } else if 
     let navigationController = rootViewController as? UINavigationController, 
     let visibleViewController = navigationController.visibleViewController { 
     // Then search navigation controller's views to find its visible controller. 
     return self.findVisibleViewController(rootViewController: visibleViewController) 
    } else if let splitViewController = rootViewController as? UISplitViewController { 
     // Then try the split view controller. This will be the true root view controller. Use the master here since the detail just shows web views. 
     return self.findVisibleViewController(rootViewController: splitViewController.viewControllers[0]) 
    } else { 
     // Guess we found the visible view controller, because none of the other conditions were met. 
     return rootViewController 
    } 
} 

findVisibleViewController(_:)は私のプロジェクトの一つの例であると私のアプリで正確なビューコントローラの階層に合わせて調整されます。あなたの階層に合った方法であなた自身のアプリのためにこれを編集する必要があります。

第3に、すべてではないにしても、ほとんどの場合、supportedInterfaceOrientations()を実装する必要があります。

最後に、これはモーダルで表示または消した状況のみを処理します。ショー(プッシュ)セグの場合、ナビゲーションコントローラーの向きは、スタックにプッシュされたすべての新しいビューコントローラーで使用されます。より細かい制御が必要な場合は、オリエンテーションを強制する必要があります。ここにその例があります:

// Some other view had the screen in landscape, so force the view to return to portrait 
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation") 
関連する問題