I have a UIView class I'm integrating into a project. I plan to add MySubView to MyViewController. There's a constant class where the screen width is hard-coded as follows:
MySubviewConstants.h
#define SCREEN_WIDTH 300.0f
I'd like to define this constant as the width of the subview of the device it's running on, not a hard-coded number. I think this should be an easy 25 pointer.
This code will get the actual device's screen width:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
Or perhaps using a variant of:
[[[[UIApplication sharedApplication] keyWindow] rootViewController].view convertRect:[[UIScreen mainScreen] bounds] fromView:nil].size
Related
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
and here is [UIscreen mainscreen].applicationFrame
[[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.
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.
Make sure you have added all resolution splash image on yous's application Images.xcassets
`
Please take a look on Screen shot i attached
`
CGRect appFrame = [[UIScreen mainScreen] bounds]; //This is the
correct way to get your screen size
I am writing a GUI, with a main menu, a second screen and a back button to the main menu. From the initial main menu I use the following lines of code:
midScreenX = [UIScreen mainScreen].bounds.size.width/2;
midScreenY = [UIScreen mainScreen].bounds.size.height/2;
WarScene *battle = [[WarScene alloc] initWithSize: CGSizeMake(midScreenX*2, midScreenY*2)];
SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0];
SKView * skView = (SKView *)self.view;
UIView *viewToRemove = [self.view viewWithTag:101];
[viewToRemove removeFromSuperview];
[skView presentScene:battle transition:reveal];
This works... I think. I open up a new scene, my second scene is the correct size and at least fills the screen. There is a node in that scene which is too big for the screen, and I am working on changing that, but I don't think that that would actually effect the UIScreen's bounds.
The problem arises when I return to the main menu with the following code:
midScreenX = [UIScreen mainScreen].bounds.size.width/2;
midScreenY = [UIScreen mainScreen].bounds.size.height/2;
GameScene *map = [[GameScene alloc] initWithSize: CGSizeMake(midScreenX*2 ,midScreenY*2)];
SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0];
SKView * skView = (SKView *)self.view;
UIView *viewToRemove = [self.view viewWithTag:3];
[viewToRemove removeFromSuperview];
[skView presentScene:map transition:reveal];
As far as I can work out, the values being passed through should be exactly the same as the values initially sent for [UIScreen mainScreen].bounds.size and yet the scene which is initialised is far too big.
Is there anything that I could be doing which might affect [UIScreen mainScreen].bounds.size? Or have I misunderstood what init and .bounds actually do?
In terms of what I have already done to try and solve this problem myself, I have looked at examples of how scenes are normally initialised. I see that often values are used in place of .bounds for example :
initWithSize: CGSizeMake(1024,768)
However, wouldn't that mean that on different devices the scene wouldn't be shown properly/fully?
if you are using iOS 6 or iOS 7,
both [UIScreen mainScreen].bounds.size.width and [UIScreen mainScreen].bounds.size.height is same in all orientation..
but in iOS 8 it give one value in landscape and give another value in portrait mode
NSLog(#"width %f",[[UIScreen mainScreen] bounds].size.width); //portrait ios7 or 6 = 320 , landscape ios7 or 6 = 320
NSLog(#"height %f",[[UIScreen mainScreen] bounds].size.height); //portrait ios7 or 6 = 568 , landscape ios7 or 6 = 568
NSLog(#"width %f",[[UIScreen mainScreen] bounds].size.width); //portrait ios8 = 320 , landscape ios8 = 568
NSLog(#"height %f",[[UIScreen mainScreen] bounds].size.height); //portrait ios8 = 568 , landscape ios8 = 320
so we can check conditions like this,
CGFloat midScreenX,midScreenY;
if (UIDeviceOrientationIsLandscape((UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation]))
{
//landscape mode
midScreenX = ([[UIScreen mainScreen]bounds].size.width>[[UIScreen mainScreen]bounds].size.height?[[UIScreen mainScreen]bounds].size.width:[[UIScreen mainScreen]bounds].size.height)/2;
midScreenY = ([[UIScreen mainScreen]bounds].size.width<[[UIScreen mainScreen]bounds].size.height?[[UIScreen mainScreen]bounds].size.width:[[UIScreen mainScreen]bounds].size.height)/2;
}
else
{
//portrait mode
midScreenX = [[UIScreen mainScreen]bounds].size.width/2;
midScreenY = [[UIScreen mainScreen]bounds].size.height/2;
}
I hope you warScene and gameScene are subclass of SKScene. The method initWithSize returns, A newly initialized scene object. and parameter 'size' is The size of the scene in points.
For more information refer https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKScene_Ref/index.html#//apple_ref/occ/instm/SKScene/initWithSize:
Ok tried almost everything and it didn't work.
I need my engine to start up in LandscapeRight mode, therefore i call:
//
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
The problem is that the rest is not rotated at all,
I managed to rotate the view by using:
[pExternViewController setTransform:CGAffineTransformMakeRotation(M_PI/2.0f)];
But it doesn't work as expected, the Frame Buffer size is correct now:
FrameBuffer: width: 960, height: 640
Still you can see it is not 960x640, and i can't figure out why?
Ok, finally made it.
First add this key to your plist file (required)
<key>UILaunchStoryboardName</key>
<string>Launch Screen</string>
Screen Size definitions
#define SCREEN_WIDTH (IOS_VERSION_LOWER_THAN_8 ? (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height) : [[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT (IOS_VERSION_LOWER_THAN_8 ? (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width) : [[UIScreen mainScreen] bounds].size.height)
#define IOS_VERSION_LOWER_THAN_8 (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1)
The window initialization
- (void) applicationDidFinishLaunching: (UIApplication*) application
{
// Start in Landscape
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
// Disable Auto Lock screen
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
// Set Bounds with the proper screen resolution
CGRect screenBounds = [[UIScreen mainScreen] bounds];
screenBounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
// Create window and view controler
m_window = [[UIWindow alloc] initWithFrame: screenBounds];
m_window.backgroundColor = [UIColor redColor];
// Create new view controller
pExternViewController = [SvgzViewController alloc] ;
// Initialize view controler with all pointers set up
if( [pExternViewController initWithFrame: screenBounds ] == nil)
{
assert(!"Failed to initialize screen");
exit(0);
}
// Rotate the window
[m_window setTransform:CGAffineTransformMakeRotation(M_PI/2.0f)];
// Set the proper window center after transformation
m_window.center = CGPointMake(screenBounds.size.height/2, screenBounds.size.width/2);
// Add GL window
[m_window addSubview: pExternViewController];
//
[m_window makeKeyAndVisible];
}
At least no GL side change has to be done by doing it this way.
I have an imageView that is resized in two different parts of my code. This works great in my textFieldDoneEditing. However, when I use the same code in my viewDidLoad, I get a different sized view. Is there a difference with doing this in the that method?
Here's the code:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGRect frame = CGRectMake(0,0, screenWidth, screenWidth);
PuzzleImage.frame = frame;
Thank you
If your ViewController is landscape mode, viewDidLoad always gets main screen's bounds in portrait mode.
So, solution is setFrame in viewWillAppear to get right bounds.
I can't take good results when I try to read resolution of my device.
Such code
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
NSString* name = [self machineName];
gives
1024*768 and iPad4,5
machineName code:
#import <sys/utsname.h>
-(NSString*) machineName()
{
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
}
What am I doing wrong? I can't understand. It must be 2048*1536
Bound returns the screen dimensions in points.
You need to query CGFloat scale = [[UIScreen mainScreen] scale];
and multiply this by the width/height to get the pixel value
Try this one to get screen resolution in points...
(1) Find the screen scale using below method
CGFloat screenScale = [[UIScreen mainScreen] scale];
This will give you the scale of the screen. For all iPhones and iPodTouches that do NOT have Retina Displays will return a 1.0f, while Retina Display devices will give a 2.0f.
Now if you want to get the pixel width & height of the iOS device screen you just need to do one simple thing.
(2) By multiplying by the screen's scale you get the actual pixel resolution.
CGSize screenSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width * screenScale, [[UIScreen mainScreen] bounds].size.height * screenScale);
For more details read this link