Playing around with iOS6 today, and I got stuck on checking view frame size under various orientations, which I thought I understood in iOS5. (I understand that auto-layout solves the basic problem in many cases, but still I feel I may need this check for various reasons?).
With auto-layout off, checking the view dimensions returns the right dimension size, but not the correct edge. That is to say in portrait on the iPhone the .width property = 320 and in landscape the .width property = 300, whereas I would expect 300 to be the value of the height dimension.
This is kind of weird to me as the self.view does scale properly to the window, but another view set to the frame of the self.view does not. This would be easy to fix by checking the orientation then getting the right numbers, I guess, but am I missing something obvious, or some new rule in iOS6?
//always gives aprox 300 for width despite device orientation
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
screenRect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
screenRect = self.view.window.frame;
NSLog(#"screen rect size = %f x %f", screenRect.size.width, screenRect.size.height);
[imageView setFrame:screenRect];
}
//a button to check orientation. always gives aprox 300 for width despite device orientation
- (IBAction)checkScreenSize:(id)sender {
screenRect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
NSLog(#"screen rect size = %f x %f", screenRect.size.width, screenRect.size.height);
screenRect = [[UIScreen mainScreen]applicationFrame];
NSLog(#"screen rect size = %f x %f", screenRect.size.width, screenRect.size.height);
}
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
In my previous question I shared my problem with the black background appearing in my app on orientation change: Black background when view rotates on orientation change
I did not manage to solve the problem by following any of the advices I got and I have the feeling that the only way I can avoid the black background is by manually rotating my subviews on orientation change?
One of my subviews is a UILabel which is supposed to cover the entire screen. The rotation is going pretty well using a line of code similar to this one:
myLabel.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(isLandscape ? 90 : 0));
My problem is to make the UILabel adjust to take up the entire screen in landscape mode as well. I have tried to switch height and width of its bounds, but nothing happens.
Any suggestions will be greatly appreciated.
Here are some more code details:
[UIView animateWithDuration:0.5
animations:^{
myLabel.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(isLandscape ? 90 : 0));
}
completion:^(BOOL finished) {
myLabel.bounds = CGRectMake(0, 0, [self screenWidth], [self screenHeight]);
CGFloat fontSize = ((isLandscape ? 0.9649 : 0.9375) * [self screenWidth]) / 2.74;
[myLabel setFont:[UIFont fontWithName:FontName size:fontSize]];
}
];
where
- (CGFloat) screenWidth {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
return isLandscape ? MAX(screenSize.width, screenSize.height) : MIN(screenSize.width, screenSize.height);
}
- (CGFloat) screenHeight {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
return isLandscape ? MIN(screenSize.width, screenSize.height) : MAX(screenSize.width, screenSize.height);
}
Perhaps you are calling sizeToFit on the label somewhere?
Try instead to set:
myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.minimumFontScale = 0.1;
This will adjust your labels font size to fit the width of label.
I found out that I was able to adjust the label size once I unchecked the auto layout checkbox.
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 have an iAd which I am trying to position at the bottom of the screen though when I try to get the height of the ad so that I can put it at the bottom. This error only occurs when I enter the screen in a landscape orientation as the ad is taller in the portrait orientation and only on the iPhone simulator as on an ipad the ads are the same height.
The method I am using to put the add at the bottom is by setting the y value of the ad frame to the height of the view minus the height of the ad.
Here is the code that I am currently using:
- (void)viewWillAppear:(BOOL)animated
{
CGRect adFrame = banner.frame;
adFrame.origin.y = screenHeight - banner.frame.size.height;
adFrame.size.width = screenWidth;
banner.frame = adFrame;
[banner setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth];
}
- (void)viewDidLoad
{
[super viewDidLoad];
banner.frame = CGRectZero;
}
Any help would be much appreciated.
I would take a look at Apple's sample code
for integrating iAD exactly how you describe, particularly in ContainerBanner/ContainerBanner/BannerViewController.m. What you are looking for is ADBannerView's - (CGSize)sizeThatFits:(CGSize)size.
CGRect adRect = CGRectZero;
CGRect contentFrame = self.view.bounds;
adRect.size = [banner sizeThatFits:contentFrame.size];
adRect.origin.y = CGRectGetHeight(contentFrame) - CGRectGetHeight(adRect);
banner.frame = adRect;
In laying out CALayer objects in the main view of a viewController I am seeing a discrepancy between IOS 43. and 5.1
I am running in landscape mode. I want a CALayer object 40 pts wide, showing at the top left -- butted up against the nav bar and the full height of the screen (less the nav bar). Status bar is disabled. I calculate the height of the layer as the height of the view less the height of the nav bar.
In viewDidLoad of my viewController I have the following code....
[[UIApplication sharedApplication] setStatusBarHidden:YES];
CGRect viewFrame = self.view.frame;
float navBarHeight = self.navigationController.navigationBar.frame.size.height;
CGRect f = CGRectMake( 0, navBarHeight, 40, viewFrame.size.height - navBarHeight);
leftLayer = [[CALayer alloc] init];
leftLayer.frame = f;
leftLayer.backgroundColor = [UIColor greenColor].CGColor; // for debug
[self.view.layer addSublayer:leftLayer];
When I run this on 5.1 viewFrame = (0, 0, 480, 288) and navBarHeight = 32. When I run this on 4.3 viewFrame = (0, 0, 480, 320) and navBarHeight = 32. I've tried the same comparison in portrait mode and there is no discrepancy between OS versions.
navBarHeight also seems wrong -- because my green CALayer object has about 12 pts of white space between it and the nav bar. What am I doing wrong?
I have better luck with using the viewWillAppear method to adjust frames because the view has been sized for the orientation by then. Do you have the same issue in that method?