5

UIWebView縦向きか横向きかに応じて異なる URL を読み込みたいと考えています。現在の方向が内側にあることをどのように把握できviewWillAppearますか?

4

4 に答える 4

14

UIApplication のstatusBarOrientationプロパティを使用します。UIDeviceOrientationFaceUpまたはのデバイスの向きを使用すると、問題が発生する可能性がありますUIDeviceOrientationFaceDown

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation))
{
   // Do something when in landscape
}
else
{
   // Do something when in portrait
}
于 2011-10-31T18:11:26.230 に答える
4
UIDeviceOrientation getCurrentOrientation() {
  UIDevice *device = [UIDevice currentDevice];
  [device beginGeneratingDeviceOrientationNotifications];
  UIDeviceOrientation currentOrientation = device.orientation;
  [device endGeneratingDeviceOrientationNotifications];

  return currentOrientation;
}

を に変換するのはあなた次第UIDeviceOrientationですUIInterfaceOrientation

于 2011-10-31T18:05:28.667 に答える
2

アプリが読み込まれるとき、現在の向きはわかりません-

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
    NSLog(@"portrait");// only works after a rotation, not on loading app
}

デバイスを回転させると正しい向きになりますが、向きを変更せずにアプリをロードすると[[UIDevice currentDevice] orientation]、現在の向きがわからないようです。

したがって、2つのことを行う必要があります-

  1. plist ファイルで、アプリケーションが受け入れるデバイスの向きを設定してみてください
  2. UIViewControllers ではshouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)、アプリが必要なときに YES を返すようにメソッドをオーバーライドする必要があります。rotate:
于 2011-10-31T17:57:49.757 に答える
1

interfaceOrientation関連する呼び出しの順序付けが、その時点で正しい値を持っていることに依存できないことを意味する場合 (viewWillAppear親ビュー コントローラーが回転すると仮定すると)、最も安全なのはおそらくself.parentViewController.interfaceOrientationです。から最初の仮定を試みることができなかった場合[[UIDevice currentDevice] orientation]、これは常に 100% の利益になるとは限りません (たとえば、アプリの起動時にデバイスがテーブルに平らに置かれている場合)。肖像画。

于 2011-10-31T18:01:27.320 に答える