{"id":2626,"date":"2017-09-27T10:58:44","date_gmt":"2017-09-27T17:58:44","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2626"},"modified":"2017-09-27T10:58:44","modified_gmt":"2017-09-27T17:58:44","slug":"statusbarorientation-deprecated","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2626","title":{"rendered":"statusBarOrientation deprecated"},"content":{"rendered":"<p>My apps use code to put objects on the screen and the layout of my apps changes depending on the orientation of the device. I had been using statusBarOrientation to determine the orientation, but it as been deprecated as of iOS 9. It still works in iOS 11 but since I have to update my apps anyway for iPhone X, I decided that now would be as good a time as any to update the code.<\/p>\n<p>I believe the reason for the deprecation is that now apps can appear in a sidebar, so they don\u2019t have a status bar. I have not enabled that behaviour in my apps, so it doesn\u2019t affect me. The recommended way to get the orientation is to look at the screen size.<\/p>\n<p>I had been using this line at the top of each file where I needed to do something depending on orientation.<\/p>\n<p><pre><code class=\"preserve-code-formatting\">\n UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;\n<\/code><\/pre><\/p>\n<p>I use a utilities class for things that I use across classes, so I decided to put it there so I could change it easily if I needed to at some point in the future.<\/p>\n<p><pre><code class=\"preserve-code-formatting\">\n+ (NSString *) orientation&nbsp;&nbsp;{\n\n&nbsp;&nbsp;&nbsp;&nbsp;CGSize screenSize = [UIScreen mainScreen].bounds.size;\n&nbsp;&nbsp;&nbsp;&nbsp;NSString *deviceOrientation = @&quot;Portrait&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;if (screenSize.height &lt; screenSize.width) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deviceOrientation = @&quot;Landscape&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;return deviceOrientation;\n}\n<\/code><\/pre><\/p>\n<p>The replacement code in each class is:<br \/>\n<pre><code class=\"preserve-code-formatting\">\nNSString *orientation = [Utilities orientation];\n<\/code><\/pre><\/p>\n<p>The enum UIInterfaceOrientation has not been deprecated, so I could have returned one of the values and not had to change any other code, but the conditionals are messy and hard to read, <\/p>\n<p><pre><code class=\"preserve-code-formatting\">\nif (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)\n<\/code><\/pre><\/p>\n<p>so I decided to return a string instead to make the code easier to read, e.g.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nif ( [orientation isEqualToString:@&quot;Portrait&quot;] ) {\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>My apps use code to put objects on the screen and the layout of my apps changes depending on the orientation of the device. I had been using statusBarOrientation to determine the orientation, but it as been deprecated as of iOS 9. It still works in iOS 11 but since I have to update my &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2626\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">statusBarOrientation deprecated<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-2626","post","type-post","status-publish","format-standard","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2626"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2626\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}