gpt4 book ai didi

ios - 设备旋转后,如何将以编程方式创建的UIButton保留在屏幕底部?

转载 作者:行者123 更新时间:2023-12-01 16:44:11 25 4
gpt4 key购买 nike

由于某种原因,我必须以编程方式而不是在.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];

换句话说,我基本上是考虑整个屏幕的高度,减去状态和导航栏的高度,然后减去这些按钮的高度以及屏幕底端的空间,以设置两个按钮的 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.xorigin.y分配什么值,该按钮始终在屏幕外或完全怪异的位置出现。很奇怪,例如,如果我为这两个属性都输入了 100的值,则该按钮不会出现在离屏幕左上角约100个点的位置,但是在我看来这是一个完全随机的地方。似乎我不知道当设备旋转时坐标系会发生什么……好吧……这是我想请您介入并保存我的应用程序的地方! :-)

PS:状态栏和导航栏保持纵向放置(客户需求,客户满意)的位置,这就是为什么它们不包含在新起点计算中的原因。

最佳答案

您需要为按钮设置适当的UIViewAutoresizingMask
看到这个answer

关于ios - 设备旋转后,如何将以编程方式创建的UIButton保留在屏幕底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21509054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com