3

Hi i am initiate a rootViewController programatically (portal only no landscape]

this is the code use

CGRect appFrame = [[UIScreen mainScreen] bounds];

But it does not give correct size. (cut down at bottom screen)

Only this code give correct

 CGRect appFrame = [UIScreen mainScreen].applicationFrame

But this code will give warning of depreciated (applicationFrame is depreciated)

(My simulator is IOS 9.3)

How to fix the problem? any help is much appreciate! thanks!

here is screenshoot when using [UIscreen mainscreen].bounds

enter image description here

and here is [UIscreen mainscreen].applicationFrame

enter image description here

Lê Khánh Vinh
  • 2,591
  • 5
  • 31
  • 77

3 Answers3

3

[[UIScreen mainScreen] bounds] gives the correct size of the entire window, but it includes the size of the status bar. If you want the size of the status bar ( you can subtract its height), you should use [[UIApplication sharedApplication] statusBarFrame] to get the frame of the status bar. Navigation bars and tab bars generally have a height of 44. So, use CGRectMake() if you need a rectangle for your view:

CGRect frame_screen = [[UIScreen mainScreen] bounds];
CGRect frame_view = CGRectMake(0,0,frame_screen.size.width,frame_screen.size.height - [[UIApplication sharedApplication] statusBarFrame].size.height);

Notice that the last argument of CGRectMake is the height, usually you can minus 44 for a tab bar or navigation bar.

EDIT: Try to log [UIScreen mainScreen].applicationFrame and [[UIScreen mainScreen] bounds] to console and see what the difference is between them. Something like

CGRect frame_screen = [[UIScreen mainScreen] bounds];
NSLog(@"x: %f, y: %f, width: %f, height: %f",frame_screen.origin.x,frame_screen.origin.y,frame_screen.size.width,frame_screen.size.height);
CGRect frame_application = [UIScreen mainScreen].applicationFrame
NSLog(@"x: %f, y: %f, width: %f, height: %f",frame_application.origin.x,frame_application.origin.y,frame_application.size.width,frame_application.size.height);

Then use that information to make [[UIScreen mainScreen] bounds frame how you need it.

Surbhi Garg
  • 414
  • 5
  • 19
simple_code
  • 707
  • 9
  • 24
  • Hi using your suggestion i still get the cut down at bottom. may be need some calculation of the starting 0,0 because cut down at bottom not on top – Lê Khánh Vinh Aug 04 '16 at 17:42
  • 1
    dont know why i need to move the Y direction like this to make correct one CGRect appFrame = CGRectMake(0,[[UIApplication sharedApplication] statusBarFrame].size.height,frame_screen.size.width,frame_screen.size.height - [[UIApplication sharedApplication] statusBarFrame].size.height); – Lê Khánh Vinh Aug 04 '16 at 17:45
  • Could you put a screenshot in your question? I don't think I quite understand what the problem is. Maybe you could add some more code to the question? – simple_code Aug 04 '16 at 17:50
  • Hi i add screen shoot for. there is a gap at bottom up use bounds – Lê Khánh Vinh Aug 04 '16 at 17:57
  • Have you added launch screen for your app? Which simulator you are running it? – RashmiG Aug 04 '16 at 18:05
  • I edited the answer to try and help you find a way to solve the problem. – simple_code Aug 04 '16 at 18:23
  • Hi both log provide same result: 2016-08-05 02:39:23.896 SingPost[17516:195253] x: 0.000000, y: 0.000000, width: 320.000000, height: 568.000000 2016-08-05 02:39:23.896 SingPost[17516:195253] x: 0.000000, y: 0.000000, width: 320.000000, height: 568.000000 – Lê Khánh Vinh Aug 04 '16 at 18:40
  • I am doing by CGRect appFrame = CGRectMake(0,[[UIApplication sharedApplication] statusBarFrame].size.height,frame_screen.size.width,frame_screen.size.height); but dont know why that work – Lê Khánh Vinh Aug 04 '16 at 18:41
  • That code looks good to me. x is measured from the left of the screen, and y is measured from the top. – simple_code Aug 04 '16 at 18:57
  • Hi do you think my solution is correct! why same log with CGRect provide different outcome – Lê Khánh Vinh Aug 05 '16 at 01:32
  • I changed the debugging code a bit and tested it, application frame prints a y of 20.00000, So your code makes sense and it is correct – simple_code Aug 05 '16 at 07:43
2

you can do the following.

CGRect rect = [UIScreen mainScreen].bounds;

CGRect screenFrame = CGRectMake(0, [[UIApplication sharedApplication] statusBarFrame].size.height, rect.size.width, rect.size.height - [[UIApplication sharedApplication] statusBarFrame].size.height);

Now you can use this screenFrame, I hope this will resolve your problem.

Surbhi Garg
  • 414
  • 5
  • 19
  • 1
    Hi do we need to minus rect.size.height - [[UIApplication sharedApplication] statusBarFrame].size.height. I found same result when using only rect.size.height. is that correct? – Lê Khánh Vinh Aug 05 '16 at 08:01
  • yes because, it will take you screen [[UIApplication sharedApplication] statusBarFrame].size.height down, so without decreasing the lower portion of the screen will be clipped. – Surbhi Garg Aug 05 '16 at 08:02
  • 1
    Thanks! the bounds include status bar so we need some calculation to get actual frame from the bounds – Lê Khánh Vinh Aug 05 '16 at 08:06
  • great you found the solution :) – Surbhi Garg Aug 05 '16 at 08:08
-1

Make sure you have added all resolution splash image on yous's application Images.xcassets `

Please take a look on Screen shot i attached

` enter image description here

CGRect appFrame = [[UIScreen mainScreen] bounds]; //This is the correct way to get your screen size

Vicky
  • 1,095
  • 16
  • 15