Changing the size of UIScreen on rotation - ios

I am making an iPad app that starts out in the portrait orientation but can rotate over to the landscape orientation. In -(void) loadView, I call the function drawView. In drawView, I have this line of code:
CGRect r = [[UIScreen mainScreen] bounds];
The only problem is that it doesn't update itself when I rotate to landscape mode, so it still thinks that the screen is in the vertical orientation and if I want a text view to extend all the across the entire screen, it cuts it off at the 768th pixel, instead of the 1024th pixel. In -(BOOL)shouldAutorotate... I have case UIInterfaceOrientationLandscapeRight: and case UIInterfaceOrientationLandscapeLeft:, and I would have to ideally place CGRect r = [[UIScreen mainScreen] bounds]; under each case, but I don't think that will work. Any suggestions? Thanks for your help!
Edit: I've even tried calling a function with
CGRect r = [[UIScreen mainScreen] bounds];
in it, but it still won't work. I place an NSLog after it and I received a response, so the app is definitely working properly and not crashing, but I am still unable to figure this out. Any ideas? Thanks for your help!

I think your problem could be solved by using the autoresizingMask of UIView;

Does your app happen to rely on plists for configuration? I've seen some apps where you can get landscape but only after you set an orientation key to false.

Related

ios7 and ios8 landscape modes - part of the screen doesn't response

I'm developing an application in iOS8 in landscape mode and everything works fine. I'm using UIKit and don't support portrait mode at all.
Im trying to customise it to iOS7 but the frame that I'm getting is always like the iPhone is in a portrait mode.
I know that the in iOS8 there was a major change with the width and height and it's different in iOS7.
I have the following method which I used to set the window n the appDelegate and when I need the frame at the ViewControllers.
I'm doing the following:
- (CGRect) screenRect {
CGRect screenRect = [UIScreen mainScreen].bounds;
if ((NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
return CGRectMake(0,0,screenRect.size.height, screenRect.size.width);
}
return screenRect;
}
The views look fine but the left/right side of the screen (depends on the device orientation) behaves like it's outside the frame. i.e doesn't response to gestures!
When I'm setting the fame to the new "screenRect" it keeps turning back to the device bounds.
Thank you very much.

Custom UIWindows do not rotate correctly in iOS 8

Get your application into landscape mode and execute the following code:
UIWindow *toastWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
toastWindow.hidden = NO;
toastWindow.backgroundColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5f];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[toastWindow removeFromSuperview];
});
In iOS 7 you will get a transparent blue overlay on top of the entire screen that disappears after 5 seconds. In iOS 8 you will get a transparent blue overlay that covers a little over half the screen
This obviously has something to do with the change Apple did in iOS 8 where the screen coordinates are now interface oriented instead of device oriented but in true Apple fashion they seem to have left a myriad of bugs in landscape mode and rotation.
I can "fix" this by checking if the device orientation is landscape and flip the width and height of the main screen bounds but that seems like a horrible hack that is going to break when Apple changes everything again in iOS 9.
CGRect frame = [[UIScreen mainScreen] bounds];
if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
frame.size.width = frame.size.height;
frame.size.height = [[UIScreen mainScreen] bounds].size.width;
}
UIWindow *toastWindow = [[UIWindow alloc] initWithFrame:frame];
toastWindow.hidden = NO;
toastWindow.backgroundColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5f];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[toastWindow removeFromSuperview];
});
Has anyone been experiencing this problem and come across a better, less brittle solution?
EDIT: I know I could just use a UIView and add it to the key window but I would like to put something on top of the status bar.
Your 'fix' isn't great for another reason, in that it doesn't actually rotate the window so that text and other subviews appear with the appropriate orientation. In other words, if you ever wanted to enhance the window with other subviews, they would be oriented incorrectly.
...
In iOS8, you need to set the rootViewController of your window, and that rootViewController needs to return the appropriate values from 'shouldAutoRotate' and 'supportedInterfaceOrientations'. There is some more about this at: https://devforums.apple.com/message/1050398#1050398
If you don't have a rootViewController for your window, you are effectively telling the framework that the window should never autoRotate. In iOS7 this didn't make a difference, since the framework wasn't doing that work for you anyway. In iOS8, the framework is handling the rotations, and it thinks it is doing what you requested (by having a nil rootViewController) when it restricts the bounds of your window.
Try this:
#interface MyRootViewController : UIViewController
#end
#implementation MyRootViewController
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotate
{
return YES;
}
#end
Now, add that rootViewController to your window after it is instantiated:
UIWindow *toastWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
toastWindow.rootViewController = [[MyRootViewController alloc]init];
I believe you have to convert the UICoordinateSpace.
On iPhone 6 Plus apps can now launch already in landscape orientation, which messed up an application I am working on since it only supports portrait orientation throughout most of the app, except one screen (meaning it needed to support landscape orientations in the plist).
The code that fixed this was as follows:
self.window = [[UIWindow alloc] initWithFrame:[self screenBounds]];
and calculating the bounds with the following code:
- (CGRect)screenBounds
{
CGRect bounds = [UIScreen mainScreen].bounds;
if ([[UIScreen mainScreen] respondsToSelector:#selector(fixedCoordinateSpace)]) {
id<UICoordinateSpace> currentCoordSpace = [[UIScreen mainScreen] coordinateSpace];
id<UICoordinateSpace> portraitCoordSpace = [[UIScreen mainScreen] fixedCoordinateSpace];
bounds = [portraitCoordSpace convertRect:[[UIScreen mainScreen] bounds] fromCoordinateSpace:currentCoordSpace];
}
return bounds;
}
Hopefully this will lead you in the right direction.
In iOS 7 and earlier, UIWindow's coordinate system did not rotate. In iOS 8 it does. I am guessing the frame supplied from [[UIScreen mainScreen] bounds] does not account for rotation, which could cause issues like what you're seeing.
Instead of getting the frame from UIScreen, you could grab the frame from the appDelegate's current key window.
However, it does not appear you really need functionality supplied by UIWindow. I'd like to echo others' recommendation that you use a UIView for this purpose. UIView is more generic than UIWindow, and should be preferred.
The best thing to do is use views instead of windows:
Most iOS applications create and use only one window during their lifetime. This window spans the entire main screen [...]. However, if an application supports the use of an external display for video out, it can create an additional window to display content on that external display. All other windows are typically created by the system
But if you think you've got a valid reason to create more than one window, I suggest you create a subclass of NSWindow that handles sizes automatically.
Also note that windows use a lot of RAM, especially on 3x retina screens. Having more than one of them is going to reduce the amount of memory the rest of your application can use before receiving low memory warnings and eventually being killed.

Xcode 6 beta and resizable Iphone: How to get the current Screen Dimensions

I try to make my Views completely dynamic so the Views can fit any Screen Dimensions. At the Moment i get the screen Width and Height for calculation like this:
CGFloat getDisplayHeight() {
CGSize result = [[UIScreen mainScreen] bounds].size;
return result.height;
}
CGFloat getDisplayWidth() {
CGSize result = [[UIScreen mainScreen] bounds].size;
return result.width;
}
The Problem is that the "Resizable Iphone" Emulator always give me a Width of 768 and a Height of 1024 (Like the iPad). It doesn´t matter when i change the Width and Height Values on the Bottom of the Emulator...is there any new Function i can use for better testing?
EDIT:
I´m still not sure how to handle this exactly...At the Moment i calculate the Size of my Subviews in "viewDidLayoutSubviews" because this is the only Method which shows the correct Width and Heigth of the "resizable Iphone Simulator", the Problem is that when i calculate all my Stuff inside this method it gets called every time when i scroll or touch the UI, so it could be the case that it gets called hundreds of times...and when i try to calculate my Views in "ViewDidAppear" its too late and evrything look reallys weird after the recalculation....

iPad Landscape messing up touches began

My app is only allowable in the landscape orientations, and it launches in landscape. Thus, the top left corner when the iPad is landscape is (0,0), so everything works fine.
However, when I pick up "touchesBegan"...it isn't working properly. Only when I tap on like the right two-thirds of the iPad does it pick up the touches. I know it has something to do with the orientation, because the app is literally just blank screen with a single UIView and nothing else. It is quite simple. There are no other possible problems that would cause this.
To be sepecific, if I print out the x location in the touchesBegan function, and if the iPad is held with the home button on the left, the width is 1024. And 1024-768 is 256. This is exactly the x position where it begins to sense my touches. Anything to the left of x=256 does not sense the touches.
How do I fix this?
Check Struts and Springs and make sure that whatever should pick up the touches is covering the whole area and locked to the 4 sides.
To do it programmatically,
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
[self.view setFrame:CGRectMake(0.0f, 0.0f, appFrame.size.width, appFrame.size.height)];
return YES;
// if you want to support only LANDSCAPE mode, use the line below
/*
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight);
*/
}
This sets the view to occupy the full screen.
The answer is that, when defining the UIWindow, it needs to be defined as
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
and not strict coordinates.

UIScreen bounds versus applicationFrame

Why do I see in some sample code from Apple (such as PhotoScroller) that I should do the following in loadView:
CGRect frame = [[UIScreen mainScreen] bounds];
instead of
CGRect frame = [[UIScreen mainScreen] applicationFrame];
Does it make a difference to get the main screen's frame?
ApplicationFrame is the screen size minus the size of the status bar (if visible), bounds is the screen size regardless of status bar.
So applicationFrame would return CGRectMake(0,0,320,460) assuming your app has the status bar set to be visible, while bounds would return CGRectMake(0,0,320,480) under the same conditions. Those numbers are assuming iPhone/iPod Touch screen sizes.
UIScreen Class Reference
iOS 9 Update
When using the Split View feature on the iPad, applicationFrame returns the size of your app window. bounds always returns the size of the device.

Resources