由于某种原因,我必须以编程方式而不是.xib / .storyboard文件创建两个UIButton,并且我希望它们始终位于屏幕的底部。我想如果我想让按钮处于完全相同的位置,如果它们是在Interface Builder中创建的,我必须像这样定义它们的框架:
CGRect captureCodeFrame = [[self captureCodeButton] frame];
CGRect captureReceiptFrame = [[self captureReceiptButton] frame];
captureCodeFrame.origin.x = MARGIN;
captureCodeFrame.origin.y = [[UIScreen mainScreen] bounds].size.height - [[UIApplication sharedApplication] statusBarFrame].size.height - [[[self navigationController] navigationBar] frame].size.height - BUTTON_HEIGHT - MARGIN;
captureReceiptFrame.origin.x = [[UIScreen mainScreen] bounds].size.width - BUTTON_WIDTH - MARGIN;
captureReceiptFrame.origin.y = [[UIScreen mainScreen] bounds].size.height - [[UIApplication sharedApplication] statusBarFrame].size.height - [[[self navigationController] navigationBar] frame].size.height - BUTTON_HEIGHT - MARGIN;
[[self captureCodeButton] setFrame:captureCodeFrame];
[[self captureReceiptButton] setFrame:captureReceiptFrame];
换句话说,我基本上取整个屏幕的高度,减去状态和导航栏的高度,然后是这些按钮的高度和屏幕下端的空间来设置{{1}两个按钮的坐标。我正在使用类似的技术来确定右键的Y
坐标。这三个宏的定义如下:
X
最后,我有方法#define MARGIN 20
#define BUTTON_HEIGHT 30
#define BUTTON_WIDTH 136
,只要用户旋转设备就会调用它,我知道我需要为按钮设置新框架以便重绘(或者我可以吗?也许是通过一些转换完成的相反?)但无论我尝试将新帧分配给什么值,我都无法做到正确。我的最新尝试看起来像这样(缩写为仅包括左横向模式):
(void)didRotate:(NSNotification *)notification
}
但无论我尝试分配给- (void)didRotate:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
CGSize newSize = [[self view] bounds].size;
CGRect captureCodeFrame = [[self captureCodeButton] frame];
CGRect captureReceiptFrame = [[self captureReceiptButton] frame];
[CATransaction begin];
[CATransaction setAnimationDuration:0.0];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self previewLayer] setPosition:CGPointMake(0.5 * newSize.width, 0.5 * newSize.height)];
switch (deviceOrientation)
{
case UIDeviceOrientationPortrait:
captureCodeFrame.origin.x = MARGIN;
captureCodeFrame.origin.y = [[UIScreen mainScreen] bounds].size.height - [[UIApplication sharedApplication] statusBarFrame].size.height - [[[self navigationController] navigationBar] frame].size.height - BUTTON_HEIGHT - MARGIN;
captureReceiptFrame.origin.x = [[UIScreen mainScreen] bounds].size.width - BUTTON_WIDTH - MARGIN;
captureReceiptFrame.origin.y = [[UIScreen mainScreen] bounds].size.height - [[UIApplication sharedApplication] statusBarFrame].size.height - [[[self navigationController] navigationBar] frame].size.height - BUTTON_HEIGHT - MARGIN;
[[self captureCodeButton] setFrame:captureCodeFrame];
[[self captureReceiptButton] setFrame:captureReceiptFrame];
[[self view] setTransform:CGAffineTransformMakeRotation(0)];
[[self previewLayer] setAffineTransform:CGAffineTransformMakeRotation(0)];
break;
case UIDeviceOrientationLandscapeLeft:
captureCodeFrame.origin.x = [[UIScreen mainScreen] bounds].size.width - BUTTON_WIDTH - MARGIN;
captureCodeFrame.origin.y = MARGIN;
// TODO: adjust captureReceiptFrame here
[[self captureCodeButton] setFrame:captureCodeFrame];
[[self view] setTransform:CGAffineTransformMakeRotation((CGFloat) (M_PI / 2))];
[[self previewLayer] setAffineTransform:CGAffineTransformMakeRotation((CGFloat) (-M_PI / 2))];
break;
case UIDeviceOrientationLandscapeRight:
// TODO: something should be here
[[self view] setTransform:CGAffineTransformMakeRotation((CGFloat) (-M_PI / 2))];
[[self previewLayer] setAffineTransform:CGAffineTransformMakeRotation((CGFloat) (M_PI / 2))];
break;
default:
break;
}
[CATransaction commit];
和origin.x
的值是什么,该按钮始终显示在屏幕外或完全奇怪的位置。很奇怪,如果我为这两个属性设置origin.y
的值,按钮不会显示距离屏幕左上角约100个点,但对于我来说似乎是一个完全随机的地方。看起来我不知道当设备旋转时坐标系会发生什么,而且......好吧......我很乐意请你介入并保存我的应用程序! : - )
PS:状态和导航栏从纵向(客户需求,客户获取)保持其位置,这就是为什么它们不包含在新原点计算中。