Search code examples
iosipadlandscape

How to determine the coordinates of iPad's bottom right corner in landscape mode?


I am trying to place a subview (buttView) in the bottom right corner in the main view (from code, not from IB).

The following code works

const float bottom = self.frame.size.height;
const float left   = self.frame.size.width;
NSLog(@"bottom=%.1f left=%.1f", bottom, left);
CGSize bz = buttView.frame.size;
CGRect rc = CGRectMake(left-bz.width, bottom-bz.height, bz.width,bz.height);
buttView.frame = rc;
[self addSubview:buttView];

But I am confused, because, according to the log message, bottom=1004.0 and left=768.0, and the iPad is in landscape mode! (The width should be larger than the height!??)

Moreover, if I turn off "autoresize subviews" in IB, then code will only work if I swap bottom and left (but it does not work perfectly because the true width is 1024 and not 1004).

My question is: is there an elegant way to determine the location of the view's bottom right corner when the view is in landscape mode?


Solution

  • If you look at those values in viewWillLayoutSubviews, and you use bounds instead of frame, you will get the correct values, and they will be updated if you rotate the device.