If I’m not setting Splash image programatically every thing fine, But I’m setting it because black screen appears if my web service taking long time. So i decided to set an image to window like this.
UIImage *splashimg;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 480)
splashimg=[UIImage imageNamed:#"Default.png"];
else if (screenBounds.size.height == 960)
splashimg=[UIImage imageNamed:#"Default#2x.png"];
else if (screenBounds.size.height == 1136)
splashimg=[UIImage imageNamed:#"Default-568h#2x.png"];
else if (screenBounds.size.height == 1334)
splashimg=[UIImage imageNamed:#"Default-667h#2x.png"];
else if (screenBounds.size.height == 2008)
splashimg=[UIImage imageNamed:#"Default-Portrait-736h#3x.png"];
else
splashimg=[UIImage imageNamed:#"Default568.png"];
}
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
if(UIDeviceOrientationPortrait)
{
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)] && [[UIScreen mainScreen] scale] == 2){
splashimg=[UIImage imageNamed:#"Default-Portrait#2x~ipad.png"];
}
else if([UIScreen mainScreen].bounds.size.height==1536 )
splashimg=[UIImage imageNamed:#"Default1024x768#2x.png"];
else if([UIScreen mainScreen].bounds.size.height==1024 )
splashimg=[UIImage imageNamed:#"Default768x1024.png"];
else {
// splashimg=[UIImage imageNamed:#"Default768x1024.png"];
splashimg=[UIImage imageNamed:#"Default-Portrait~ipad.png"];
}
}
else
{
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)] && [[UIScreen mainScreen] scale] == 2){
splashimg=[UIImage imageNamed:#"Default-Landscape#2x~ipad.png"];
}
else
{
splashimg=[UIImage imageNamed:#"Default-Landscape~ipad.png"];
}
}
}
splashView=[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
float width = ((UIScreen *)[UIScreen mainScreen]).bounds.size.width;
float height = (((UIScreen *)[UIScreen mainScreen]).bounds.size.height);
splashView.frame=CGRectMake(0,0, width, height);
splashView.image=splashimg;
[self.window addSubview:splashView];
[self.window bringSubviewToFront:splashView];
[self performSelector:#selector(LoadViewnearby) withObject:self afterDelay:0.1];
// here is a web service Which is taking long time, So my window appears white,so instead of white i want to show the image which is same as splash.
I want to do exactly like splash. But my image view is getting compressed horizontally when compared to original splash.
Please help me…
Thank you..
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:
We have an app-which is ONLY landscape, that works great on ALL apple devices .
It also has the launch images .
We are lost with this bug.
We have set the app for landscape only in the device orientation menu.
All views are positioning in code only.
What happens on iPad2(iOS7) ONLY , is when you switch between views, you see for a second a portrait mode (which looks bad because graphics is for landscape only-so image is on half screen) and than it switch back to the landscape back after a second.
For some other views , the iPad2 ONLY stay forever on portrait for some reason -that means we see the graphics on half screen .its not positioned right .
the size of the screen that we get is just fine
float width=[UIScreen mainScreen].bounds.size.width;
float height=[UIScreen mainScreen].bounds.size.height;
768/1024
**** views that made with storyboard are ok- the others that positioning made with software are bad.
I use the following code (based on link given in above comment):
BOOL OSIsBelowIOS8 = [[[UIDevice currentDevice] systemVersion] floatValue] < 8.0;
if(OSIsBelowIOS8) {
if( [ [ UIScreen mainScreen ] respondsToSelector: #selector( nativeBounds ) ] )
{
self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] nativeBounds]];
}
else
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
}
} else {
self.window = [[UIWindow alloc] initWithFrame: [self screenBoundsOrientationDependent] ];
}
and this is my screenBoundsOrientationDependent
//Always return the iOS8 way - i.e. height is the real orientation dependent height
- (CGRect)screenBoundsOrientationDependent {
UIScreen *screen = [UIScreen mainScreen];
CGRect screenRect;
if (![screen respondsToSelector:#selector(fixedCoordinateSpace)] && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
screenRect = CGRectMake(screen.bounds.origin.x, screen.bounds.origin.y, screen.bounds.size.height, screen.bounds.size.width);
} else {
screenRect = screen.bounds;
}
return screenRect;
}
Bonjour,
My iOS project support landscape and run perfectly on my iPhone 4s 7.1.2 but not in the simulator.
Here some screenshot :
iPhone Landscape
Simulator Landscape
Both screenshots have been taken in landscape mode and the simulator does not seem to resize the board correctly
Here the code I use to change orientation.
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
[UIView animateWithDuration:duration animations:^{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
int width = [[UIScreen mainScreen] bounds].size.width - [[UIApplication sharedApplication] statusBarFrame].size.width - 10;
int height = width;
int x = 5;
int y = ([[UIScreen mainScreen] bounds].size.width / 2) - (width / 2);
[board setFrame:CGRectMake(x, y, width, height)];
}];
}
else {
[UIView animateWithDuration:duration animations:^{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[board setFrame:CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.width)];
}];
}
}
Thanks for your help :)
I haven't tried your code but I strongly believe what you see is caused by the deprecated API. Official documentation here gives details, and the new API that does what you want is viewWillTransitionToSize:withTransitionCoordinator:
Also there are some other questions on stackoverflow like this talking about solutions: willAnimateRotationToInterfaceOrientation not called on ios6/7
I'm trying to programmatically determine the current height and width of my application. I use this:
CGRect screenRect = [[UIScreen mainScreen] bounds];
But this yields a width of 320 and a height of 480, regardless of whether the device is in portrait or landscape orientation. How can I determine the current width and height (i.e. dependent upon the device orientation) of my main screen?
You can use something like UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) to determine the orientation and then use the dimensions accordingly.
HOWEVER, during an orientation change like in UIViewController's
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
Use the orientation passed in toInterfaceOrientation since the UIApplication's statusBarOrientation will still point to the old orientation as it has not yet changed (since you're inside a will event handler).
Summary
There are several related posts to this, but each of them seem to indicate that you have to:
Look at [[UIScreen mainScreen] bounds] to get the dimensions,
Check what orientation you are in, and
Account for the status bar height (if shown)
Links
Iphone: Get current view dimensions or screen dimensions
IPhone/IPad: How to get screen width programmatically?
Objective C - how to get current screen resolution?
“Incorrect” frame / window size after re-orientation in iPhone or iPad
iPhone Dev SDK - get screen width
Working Code
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 willRotateToInterfaceOrientation:duration:.
#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
To use the code simple call [UIApplication currentSize]. 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.
Other thoughts
You could go about getting the dimensions by looking at the UIWindow's rootViewController 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:
(gdb) po [[[[UIApplication sharedApplication] keyWindow]
rootViewController] view]
<UILayoutContainerView: 0xf7296f0; frame =
(0 0; 320 480); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H;
layer = <CALayer: 0xf729b80>>
(gdb) po [[[[UIApplication
sharedApplication] keyWindow] rootViewController] view]
<UILayoutContainerView: 0xf7296f0; frame = (0 0; 320 480); autoresize
= W+H; layer = <CALayer: 0xf729b80>>
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: [[[[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] subviews] objectAtIndex:0] frame]. That looks pretty intense on one line, but you get the idea.
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!
This is my solution code !This method can add to NSObject class's Categroy , or you can define a Top custom UIViewController class , and let all of your other UIViewControllers to inherit it .
-(CGRect)currentScreenBoundsDependOnOrientation
{
CGRect screenBounds = [UIScreen mainScreen].bounds ;
CGFloat width = CGRectGetWidth(screenBounds) ;
CGFloat height = CGRectGetHeight(screenBounds) ;
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
screenBounds.size = CGSizeMake(width, height);
}else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
screenBounds.size = CGSizeMake(height, width);
}
return screenBounds ;
}
Note, after IOS8 , as Apple Document of UIScreen's bounds property says :
Discussion
This rectangle is specified in the current coordinate space, which takes into account any interface rotations in effect for the device. Therefore, the value of this property may change when the device rotates between portrait and landscape orientations.
so for the consideration of compatibility , we should detect the IOS version and make the change as below:
#define IsIOS8 (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
-(CGRect)currentScreenBoundsDependOnOrientation
{
CGRect screenBounds = [UIScreen mainScreen].bounds ;
if(IsIOS8){
return screenBounds ;
}
CGFloat width = CGRectGetWidth(screenBounds) ;
CGFloat height = CGRectGetHeight(screenBounds) ;
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
screenBounds.size = CGSizeMake(width, height);
}else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
screenBounds.size = CGSizeMake(height, width);
}
return screenBounds ;
}
Here's a handy macro:
#define SCREEN_WIDTH (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)
#define SCREEN_HEIGHT (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)
In iOS 8+ you should use the viewWillTransitionToSize:withTransitionCoordinator method:
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
// You can store size in an instance variable for later
currentSize = size;
// This is basically an animation block
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Get the new orientation if you want
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
// Adjust your views
[self.myView setFrame:CGRectMake(0, 0, size.width, size.height)];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Anything else you need to do at the end
}];
}
This replaces the deprecated animation method that gave no information about size:
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
As of iOS 8 screen bounds are now returned correct for current orientation. This means an iPad in landscape orientation [UIScreen mainScreen].bounds would return 768 on iOS <=7 and 1024 on iOS 8.
The following returns the correct height and width on all versions released.
-(CGRect)currentScreenBoundsDependOnOrientation
{
NSString *reqSysVer = #"8.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
return [UIScreen mainScreen].bounds;
CGRect screenBounds = [UIScreen mainScreen].bounds ;
CGFloat width = CGRectGetWidth(screenBounds) ;
CGFloat height = CGRectGetHeight(screenBounds) ;
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
screenBounds.size = CGSizeMake(width, height);
NSLog(#"Portrait Height: %f", screenBounds.size.height);
}else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
screenBounds.size = CGSizeMake(height, width);
NSLog(#"Landscape Height: %f", screenBounds.size.height);
}
return screenBounds ;
}
if you want the orientation dependent size and you have a view, you can just use:
view.bounds.size
I wrote category for UIScreen, that works on all iOS versions, so you can use it like this:
[[UIScreen mainScreen] currentScreenSize].
#implementation UIScreen (ScreenSize)
- (CGSize)currentScreenSize {
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBounds.size;
if ( NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1 ) {
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if ( UIInterfaceOrientationIsLandscape(interfaceOrientation) ) {
screenSize = CGSizeMake(screenSize.height, screenSize.width);
}
}
return screenSize;
}
#end
Here is a Swift way to get orientation dependent screen sizes:
var screenWidth: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.width
} else {
return UIScreen.mainScreen().bounds.size.height
}
}
var screenHeight: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.height
} else {
return UIScreen.mainScreen().bounds.size.width
}
}
var screenOrientation: UIInterfaceOrientation {
return UIApplication.sharedApplication().statusBarOrientation
}
These are included as a standard function in a project of mine:
https://github.com/goktugyil/EZSwiftExtensions
float msWidth = [[UIScreen mainScreen] bounds].size.width*(IS_RETINA?2.0f:1.0f);
float msHeight = [[UIScreen mainScreen] bounds].size.height*(IS_RETINA?2.0f:1.0f);
if ( UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ) {
os->setWidth(MIN(msWidth, msHeight));
os->setHeight(MAX(msWidth, msHeight));
} else {
os->setWidth(MAX(msWidth, msHeight));
os->setHeight(MIN(msWidth, msHeight));
}
NSLog(#"screen_w %f", os->getWidth());
NSLog(#"screen_h %f", os->getHeight());
However, on iOS 8.0.2:
+ (NSUInteger)currentWindowWidth
{
NSInteger width = 0;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
CGSize size = [UIScreen mainScreen].bounds.size;
// if (UIInterfaceOrientationIsLandscape(orientation)) {
// width = size.height;
// } else {
width = size.width;
// }
return width;
}
use -> setNeedsDisplay() for the view you want to resize.
Some improvements on the answers offered here, in Swift:
let interfaceOrientation = UIApplication.shared.statusBarOrientation // (< iOS 13)
let screenSize = UIScreen.main.bounds.size
let screenWidth = interfaceOrientation.isPortrait ? screenSize.width : screenSize.height
let screenHeight = interfaceOrientation.isPortrait ? screenSize.height : screenSize.width