UIWebView
縦向きか横向きかに応じて異なる URL を読み込みたいと考えています。現在の方向が内側にあることをどのように把握できviewWillAppear
ますか?
4 に答える
UIApplication のstatusBarOrientation
プロパティを使用します。UIDeviceOrientationFaceUp
またはのデバイスの向きを使用すると、問題が発生する可能性がありますUIDeviceOrientationFaceDown
。
例
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation))
{
// Do something when in landscape
}
else
{
// Do something when in portrait
}
UIDeviceOrientation getCurrentOrientation() {
UIDevice *device = [UIDevice currentDevice];
[device beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation currentOrientation = device.orientation;
[device endGeneratingDeviceOrientationNotifications];
return currentOrientation;
}
を に変換するのはあなた次第UIDeviceOrientation
ですUIInterfaceOrientation
。
アプリが読み込まれるとき、現在の向きはわかりません-
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
NSLog(@"portrait");// only works after a rotation, not on loading app
}
デバイスを回転させると正しい向きになりますが、向きを変更せずにアプリをロードすると[[UIDevice currentDevice] orientation]
、現在の向きがわからないようです。
したがって、2つのことを行う必要があります-
- plist ファイルで、アプリケーションが受け入れるデバイスの向きを設定してみてください
- UIViewControllers では
shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
、アプリが必要なときに YES を返すようにメソッドをオーバーライドする必要があります。rotate:
interfaceOrientation
関連する呼び出しの順序付けが、その時点で正しい値を持っていることに依存できないことを意味する場合 (viewWillAppear
親ビュー コントローラーが回転すると仮定すると)、最も安全なのはおそらくself.parentViewController.interfaceOrientation
です。から最初の仮定を試みることができなかった場合[[UIDevice currentDevice] orientation]
、これは常に 100% の利益になるとは限りません (たとえば、アプリの起動時にデバイスがテーブルに平らに置かれている場合)。肖像画。