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.
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.
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.
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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With