27429

Height and width for landscape mode is showing wrong

Question:

I have used this method for detecting the width and height of screen. But its showing the width as 768 and height as 1024 in portrait and landscape also.

CGRect screenBounds = [[UIScreen mainScreen] bounds]; float widthfloat= screenBounds.size.width; float heightfloat= screenBounds.size.height; NSLog(@" width float %f",widthfloat); NSLog(@"height float %f",heightfloat); NSLog(@"width view %f \n height view %f", self.view.bounds.size.width, self.view.bounds.size.height); NSLog(@"width %f \n height %f", screenBounds.size.width, screenBounds.size.height); float wvalue = [[UIScreen mainScreen] bounds].size.width; float hvalue = [[UIScreen mainScreen] bounds].size.height; NSLog(@" wvalue %f",wvalue); NSLog(@"hvalue %f",hvalue); CGFloat width1 = [UIScreen mainScreen].bounds.size.width; CGFloat height1 = [UIScreen mainScreen].bounds.size.height; NSLog(@"width1 %f",width1); NSLog(@"height1 %f",height1); CGFloat screenScale = [[UIScreen mainScreen] scale]; CGRect screenBounds1 = [[UIScreen mainScreen] bounds]; CGSize screenSize = CGSizeMake(screenBounds1.size.width * screenScale, screenBounds1.size.height * screenScale); if (screenSize.height==1136.000000) NSLog(@"abcd1 %f",screenBounds1.size.width); NSLog(@"abcd2 %f",screenBounds1.size.height); NSLog(@"efgh1 %f",screenSize.width); NSLog(@"efgh2 %f",screenSize.height);

Answer1:

<strong>bounds</strong> contains the bounding rectangle of the screen, measured in points. It does not care about screen orientation.

Answer2:

You can calculate the screen size, I do something like this for me -

-(CGSize)screenSize { CGSize size = [UIScreen mainScreen].applicationFrame.size; if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) return size; else { if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { if (size.height > 480.0) // This is iPhone-5 size = CGSizeMake(568.0, 320.0); else // This is iPhone-4 size = CGSizeMake(480.0, 320.0); } else { // This is iPad size = CGSizeMake(1024.0, 768.0); } if (![[UIApplication sharedApplication] isStatusBarHidden]) size.height -= 20.0; return size; } }

<strong>Note: The method will give you the height after reducing the height of the statusbar if available.</strong>

Recommend

  • R For Loops Column Means
  • Why does my code break when using a hash symbol, instead of a hash string?
  • how to remove particular words from strings?
  • iOS10.3.x Can't set nowPlayingItem on MPMusicPlayer for Apple Music tracks
  • about WifiManager.bundle
  • Calling base class functions through derived pointers
  • How to trigger methods with UIButtonBarItem?
  • NSFileHandle from NSURL failure
  • Call same +load implementation on all subclasses
  • iPhone 5: AVCaptureExposureModeAutoFocus not supported in iOS 7
  • Getting NSDate from NSString
  • How to set view of programatically created view controller to SKView?
  • How do I know if text exceeds the bounds of a text view?
  • How can i get the locale from currency names list
  • TouchesEnded not working in iOS5, working fine in iOS4
  • I am sending dictionary in json and its have two array and multiple value but i am getting response
  • iPhone App how to post Youtube video link using facebook graph Api
  • Store and fetch documents in/from app documents folder
  • running out of memory from for loop
  • how to specify height of row?
  • Detecting Ctrl + Return or Ctrl + Enter presses
  • Converting NSString to Float - Adds in Decimal Places?
  • Drag and drop without removing UIButton
  • applicationWillTerminate not called
  • Can't add target for UIButton - unrecognised selector sent to instance, despite method been in
  • Cocos2D set sprite position in relation to another sprite
  • XMPP push notifications causing problems (delay + duplications) in messages
  • Add custom information to HockeyApp crash report
  • How to use CKFetchNotificationChangesOperation?
  • Adjust UILabel Dynamically Within UITableViewCell?
  • Converting Long 64-bit Decimal to Binary
  • a simple person class in objective c
  • Why won't my UITableViewCell deselect and update its text?
  • CFNetwork SSLHandshake failed (-9806) & (-9800) & (-9830)
  • Saving Changes After In-App Purchase Has Been Purchased
  • QLPreviewController hide print button in ios6
  • Cannot Parse HTML Data Using Android / JSOUP
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Getting Messege Twice Using IMvxMessenger
  • How to load view controller without button in storyboard?