Reputation: 6112
I am having an issue with Landscape mode in my iPad application.
I created a very small new project to show my issue I set UIInterfaceOrientation in the pList to UIInterfaceOrientationLandscapeRight
In app delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window makeKeyAndVisible];
MyController *myController = [[MyController alloc] init];
[self.window addSubview:myController.view];
return YES;
}
In MyController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"Bounds Height:%f %f", self.view.bounds.size.height, self.view.bounds.size.width);
}
I have also tried putting this in viewDidLoad with same results
If I start the application while holding the device in landscape orientation the NSLog outputs
Bounds Height: 1004.000000 Bounds Width: 768.000000
What do I need to do to get the correct results? I am new to this iOS programming, all I am trying to do is anchor a UISlider to the bottom of the screen but when I am getting the incorrect coordinates I am unsure how to do it.
Upvotes: 24
Views: 18604
Reputation: 1
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect r = self.view.frame;
CGFloat width = r.size.width;
CGFloat height = r.size.height;
r.size.height = (UIInterfaceOrientationIsLandscape(orientation)) ? MIN(width, height): MAX(width, height);
r.size.width = (UIInterfaceOrientationIsLandscape(orientation)) ? MAX(width, height): MIN(width, height);
self.view.frame = r;
Upvotes: 0
Reputation: 2846
I had the same thing happen to me.
You have to spoon-feed iOS and put this line of code here or else it will give you the wrong answer.
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//required because device orientation will give you the wrong values
[UIViewController attemptRotationToDeviceOrientation];
int orientation = [[UIDevice currentDevice] orientation];
BOOL isPortrait = false;
if (orientation == 3 || orientation == 4)
isPortrait = false;
else
isPortrait = true;
NSLog(@"is portrait %i ?", isPortrait);
}
Upvotes: 0
Reputation: 1635
One of the comments on the accepted answer contained the solution to the problem for me.
There is a method you can override on UIViewController called "viewDidLayoutSubviews", I tapped into it and adjusted my containing views from there.
And since its called after "viewWillAppear" and before "viewDidAppear" the changes you make to the subviews show up as expected, no glitches.
Upvotes: 1
Reputation: 1510
Apart from viewDidAppear:
make sure using _window.rootViewController
instead of [_window addSubview:_rootViewController.view]
. That also solved my issues on iOS6.
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_rootViewController = [[MyRootViewController alloc] init];
_window.rootViewController = _rootViewController;
[_window makeKeyAndVisible];
return YES;
}
MyRootViewController.m:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"bounds: %@", NSStringFromCGRect(self.view.bounds));
UIView *myView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:myView];
}
Upvotes: 2
Reputation: 488
I created a small project to test frames and bounds because I was having the same problem. In fact checking for bounds on viewDidLoad resolved my issue.
I kept digging a little bit and I found out that the bounds are also correct on viewWillLayoutSubviews method. So I guess the most "correct" way to layout your views in a project would be:
alloc and instantiate them on viewDidLoad or loadView with frames that you think it's correct by that time.
re-layout your created views on viewWillLayoutSubviews so they are on the correct position when the view actually appear.
Upvotes: 1
Reputation: 1396
viewWillAppear (according to Paul Hegarty's CS193p lectures) is for geometry-related initializations. Since viewDidAppear follows viewWillAppear it makes sense that the bounds are correct here as well. The views bounds are not yet set in viewDidLoad.
Upvotes: 2
Reputation: 3659
I had the same problem and hacked it like so:
- (CGSize)getRotatedViewSize
{
BOOL isPortrait = UIInterfaceOrientationIsPortrait(self.interfaceOrientation);
float max = MAX(self.view.bounds.size.width, self.view.bounds.size.height);
float min = MIN(self.view.bounds.size.width, self.view.bounds.size.height);
return (isPortrait ?
CGSizeMake(min, max) :
CGSizeMake(max, min));
}
Upvotes: 11
Reputation: 4089
I dont think the original question was answered here because Im experiencing the same issue.
The key here is that if the simulator / device is in Landscape mode, and Then you start your program, self.view.(frame or bounds) retrieves the Portrait height and width. If your program is already running and then you rotate it gives the correct value. This only occurs when the device is started in Landscape and is not rotated.
Has anyone else found a solution to this or knows what Im doing wrong? Please & Thank you.
Possible Solution
I had previously been calling my method from the viewDidLoad method. It turns out that the view was still transitioning. I had better luck by calling my function from viewDidAppear that is called after everything is done.
Hope that helps.
Upvotes: 58
Reputation: 17014
You're checking the frame and bounds size too soon.
Instead, check them after rotation:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
NSLog(@"Bounds %@", NSStringFromCGRect(self.view.bounds));
NSLog(@"Frame %@", NSStringFromCGRect(self.view.frame));
}
(Note my use of NSStringFromCGRect
-- handy!)
This produces the output:
Bounds {{0, 0}, {1024, 748}}
Frame {{0, 0}, {748, 1024}}
So in this output the frame is 'wrong', but the bounds are what you expect. In fact, the frame isn't actually wrong, that's just how frame -> bounds calculations happen. So you need to access the bounds.
See also perhaps Do I have the right understanding of frames and bounds in UIKit?
N.B. viewDidAppear gets called sooner than you think in the scheme of things. According to Apple docs: "viewDidAppear notifies the view controller that its view was added to a window." In other words, it can happen before any rotation is applied.
Upvotes: 12