まーぽんって誰がつけたの?

iOS→Scala→インフラなおじさん技術メモ

iOS8でinterfaceOrientaionがdeprecatedになったんだけど代わりのプロパティが分からない

間違ってたのでいろいろ追記

Xcode6でUIViewControllerinterfaceOrientationプロパティを使うとiOS 8.0からdeprecatedですよのwarningがでます。 でも、代わりに判断できるプロパティがiPadだと分からないって話です。誰か教えて

※ 追記1 statusBarOrientationでOK

なんかUIViewControllerのプロパティでなんとかしなきゃいけないという思いにとらわれてしまっていた。statusBarOrientationを使って以下のようにやる。

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

BOOL isLandscape = UIInterfaceOrientationIsLandscape(orientation);

ios - Different ways of getting current interface orientation? - Stack Overflow


公式のReferenceとか、Stackoverflow見ると、 viewWillTransitionToSize:withTransitionCoordinator:とかtraitCollectionを使えと書いてありました。

ios8 - How to know current interfaceOrientation in extension of iOS 8? - Stack Overflow

※ 追記2 このStackOverflowは、App Extensionsの話でした。App Extensionsでは、[UIApplication sharedApplication]がとれないからどうすればいいですか?という話でした


ただ、iPadだと実際に、そこで縦なのか横なのかって判断できるプロパティがないんです。
UIViewControllerでtraitCollectionのプロパティの値がiPadが縦だろうが、横だろうが変わりません。

一番それっぽいプロパティのhorizontalSizeClassverticalSizeClassも縦だろうが横向きだろうが、どっちにしろ、UIUserInterfaceSizeClassRegularになっちゃいます。

NSLog(@"%d", self.traitCollection.horizontalSizeClass); //  => 2
NSLog(@"%d", self.traitCollection.verticalSizeClass); //  => 2

うーん。そもそも向きというものを捉えてアプリを作ろうという概念自体が間違ってるんだとは思うんだけど、とりあえずこんな感じでviewのサイズで判断するようにして回避。

※ 追記3 下のコード間違い。 statusBarOrientationを使う

if (CGRectGetWidth(self.view.frame) > CGRectGetHeight(self.view.frame)) {
    // 横向き
} else {
    // 縦
}