Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use something like <code>UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)</code> to determine the orientation and then use the dimensions accordingly.</p> <p>HOWEVER, during an orientation change like in UIViewController's </p> <pre><code>- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration </code></pre> <p>Use the orientation passed in <code>toInterfaceOrientation</code> since the UIApplication's statusBarOrientation will still point to the old orientation as it has not yet changed (since you're inside a <code>will</code> event handler).</p> <p><strong>Summary</strong></p> <p>There are several related posts to this, but each of them seem to indicate that you have to:</p> <ol> <li>Look at <code>[[UIScreen mainScreen] bounds]</code> to get the dimensions, </li> <li>Check what orientation you are in, and </li> <li>Account for the status bar height (if shown)</li> </ol> <p><strong>Links</strong></p> <ul> <li><a href="https://stackoverflow.com/q/4614284/590956">Iphone: Get current view dimensions or screen dimensions</a></li> <li><a href="https://stackoverflow.com/q/3655104/590956">IPhone/IPad: How to get screen width programmatically?</a></li> <li><a href="https://stackoverflow.com/q/1868677/590956">Objective C - how to get current screen resolution?</a></li> <li><a href="https://stackoverflow.com/q/2686882/590956">“Incorrect” frame / window size after re-orientation in iPhone or iPad</a></li> <li><a href="http://www.iphonedevsdk.com/forum/iphone-sdk-development/5793-get-screen-width.html" rel="nofollow noreferrer">iPhone Dev SDK - get screen width</a></li> </ul> <p><strong>Working Code</strong></p> <p>I usually don't go this far, but you piqued my interest. The following code should do the trick. I wrote a Category on UIApplication. I added class methods for getting the currentSize or the size in a given orientation, which is what you would call in UIViewController's <code>willRotateToInterfaceOrientation:duration:</code>.</p> <pre><code>@interface UIApplication (AppDimensions) +(CGSize) currentSize; +(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation; @end @implementation UIApplication (AppDimensions) +(CGSize) currentSize { return [UIApplication sizeInOrientation:[UIApplication sharedApplication].statusBarOrientation]; } +(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation { CGSize size = [UIScreen mainScreen].bounds.size; UIApplication *application = [UIApplication sharedApplication]; if (UIInterfaceOrientationIsLandscape(orientation)) { size = CGSizeMake(size.height, size.width); } if (application.statusBarHidden == NO) { size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height); } return size; } @end </code></pre> <p>To use the code simple call <code>[UIApplication currentSize]</code>. Also, I ran the above code, so I know it works and reports back the correct responses in all orientations. Note that I factor in the status bar. Interestingly I had to subtract the MIN of the status bar's height and width.</p> <p>Hope this helps. :D</p> <p><strong>Other thoughts</strong></p> <p>You could go about getting the dimensions by looking at the UIWindow's <code>rootViewController</code> property. I've looked at this in the past and it similarly reports the same dimensions in both portrait and landscape except it reports having a rotate transform:</p> <blockquote> <p><strong>(gdb)</strong> po [[[[UIApplication sharedApplication] keyWindow] rootViewController] view]</p> <p>&lt;UILayoutContainerView: 0xf7296f0; frame = (0 0; 320 480); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = &lt;CALayer: 0xf729b80&gt;&gt;</p> <p><strong>(gdb)</strong> po [[[[UIApplication sharedApplication] keyWindow] rootViewController] view]</p> <p>&lt;UILayoutContainerView: 0xf7296f0; frame = (0 0; 320 480); autoresize = W+H; layer = &lt;CALayer: 0xf729b80&gt;&gt;</p> </blockquote> <p>Not sure how your app works, but if you aren't using a navigation controller of some kind, you could have a UIView under your main view with the max height / width of parent and grows / shrinks with parent. Then you could do: <code>[[[[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] subviews] objectAtIndex:0] frame]</code>. That looks pretty intense on one line, but you get the idea.</p> <p>However... It would still be better to do the above 3 steps under the summary. Start messing with UIWindows and you'll find out weird stuff, like showing a UIAlertView will change UIApplication's keywindow to point at a new UIWindow that the UIAlertView created. Who knew? I did after finding a bug relying on keyWindow and discovering that it changed like that!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload