2017-10-09 5 views
0

オリエンテーションを変更した後にUIScreen.main.bounds.heightを照会すると、奇妙な結果が得られます。これは正しい方法ではないかもしれません。方向変更後の画面の高さを取得する方法は?

私はNSNotification.Name.UIDeviceOrientationDidChangeイベントをリッスンオブザーバを持っている:

NotificationCenter.default.addObserver(self, selector: #selector(self.orientationChange), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) 

これは新しい画面の高さの75%に制約を設定する関数を呼び出します。これはiPhone上で問題なく動作しますが、iPadでは画面の高さが正しく表示されません。

iPadは風景配向にある場合UIScreen.main.bounds.height肖像方向とその逆に高さに等しい値を返します。

func orientationChange() { 
    // This will print the correct value on iPhone and iPad. 
    if UIDevice.current.orientation.isLandscape { 
     print("Landscape") 
    } else { 
     print("Portrait") 
    } 

    let screenSize = UIScreen.main.bounds 
    let screenHeight = screenSize.height 
    self.searchViewHeightConstraint.constant = screenHeight * 0.75 

    // Correct value on iPhone. Incorrect on iPad. 
    print("screenHeight: '\(screenHeight)'") 

    UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: { 
     self.searchView.superview?.layoutIfNeeded() 
    }, completion: nil) 
} 

私はまた、姿勢の変化を監視するviewWilltransition方法に遭遇しましたが、これは上記の方法と全く逆のように動作します。すなわち、高さは、iPhone上でiPad上では正しいが間違っています:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 
    let screenSize = UIScreen.main.bounds 
    let screenHeight = screenSize.height 
    self.searchViewHeightConstraint.constant = screenHeight * 0.75 

    // Correct value on iPad. Incorrect on iPhone. 
    print("screenHeight: '\(screenHeight)'") 
} 

何iPhoneとiPadの間のこの矛盾した行動の理由があるとNotificationCenter姿勢変化を監視への正しいアプローチを使用していますか?

+0

「screenHeight」の値が大きくなったり小さくなったりしていますか? – Srdjan

+0

screensize = UIScreen.main.boundsの使用を中止し、代わりにサイズを使用すると、メソッドの中に与えられます。あなたはすることができますsize.height * 0.75 –

+0

また、制約を使用していて、searchViewがビューの高さに等しい場合、同じheighViewConstraintでは、常にそのサイズになる場合は、修飾子を0.75に設定できます。それからあなたが得るものは回転しているので、ビュー自体は更新されますし、ロジックをまったく行う必要はありません。 –

答えて

1

あなたが使用する必要があります viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)です。これにより、サイズがわかり、通知を使用するよりも信頼性が高くなります。あなたがUIViewControllerTransitionCoordinatorの内部で、アニメーションをやってみたかった場合

はまた、あなたが方法これはiPadとiPhoneの両方のためにスウィフト2.3に私の作品animate(alongsideTransition animation: ((UIViewControllerTransitionCoordinatorContext) -> Swift.Void)?, completion: ((UIViewControllerTransitionCoordinatorContext) -> Swift.Void)? = nil) -> Bool

+0

私はすでにこれを試して、それは私の質問のコードの正反対の動作します。すなわち、それはiPad上で正しい次元を報告するが、iPhone上では間違った次元を報告する。 – Turnip

+0

アニメーションを使用しようとしましたか(トランジションと並んでいますか?)私はこれを使用しており、その方法が完了した時点ですべてがすべて収まったようです。 –

+0

私はテストプロジェクトを作成し、iPhone用のサイズをプリントアウトしたとき、「風景(736.0,414.0)」と「ポートレート(414.0,736.0)」を取得し、その後iPad用に「風景(1024.0,768.0)と 'portrait(768.0、1024.0)'です。これらはiPad Air 2とiPhone 8でした。 –

0

に活用することができます。

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { 
    print(size.height) 

    if UIDevice.currentDevice().orientation.isLandscape { 
    print("Landscape") 
    print(size.height) 

    } 
else { 
     print("Portrait") 
     print(size.height) 
    } 

} 
関連する問題