Iphone 如何在iOS应用程序中以编程方式对齐按钮?

Iphone 如何在iOS应用程序中以编程方式对齐按钮?,iphone,objective-c,ios,Iphone,Objective C,Ios,如何通过编程方式对齐正常的按钮,如顶部、底部或中间的按钮?您应该设置其框架 差不多 UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(320, 120, 35, 10)]; 或 请记住,第一对值分别是视图的宽度和高度(X和Y中的位置),第二对值是按钮本身的宽度和高度 所以,如果你想在上面有一个按钮,你应该键入 button.frame = CGRectMake(0, 0, 35, 10); 中间的 button.fra

如何通过编程方式对齐正常的按钮,如顶部、底部或中间的按钮?

您应该设置其框架

差不多

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(320, 120, 35, 10)];

请记住,第一对值分别是视图的宽度和高度(X和Y中的位置),第二对值是按钮本身的宽度和高度

所以,如果你想在上面有一个按钮,你应该键入

button.frame = CGRectMake(0, 0, 35, 10);
中间的

button.frame = CGRectMake(160, 240, 35, 10);
底部

button.frame = CGRectMake(320, 480, 35, 10);

设置子视图位置的唯一方法是手动。您可以设置帧,如下所示,例如:

或者更改原点,例如:

view.frame.origin.x=INT\u值

获得某种对齐方式的唯一方法是使用
self.view.frame
进行数学运算。如果要将对象与屏幕中间水平对齐,请使用以下命令:

view.frame.origin.x=self.view.frame.size.width/2

如果要将某个对象与顶部对齐,且边距为44像素,请使用以下命令:

view.frame.origin.y=self.view.frame.origin.y=(view.frame.size.height/2)+44


等等。

您可以设置按钮的中心以相应地放置它

yourButton.center = CGPointMake(0.0, 0.0);// for topleft

yourButton.center = CGPointMake(160.0, 240.0);// for center

yourButton.center = CGPointMake(320.0, 480.0);// for bottomright

希望这有助于你设置你自己的框架

水平对齐

float X_Co = (self.view.frame.size.width - yourButtonWidth)/2;
[button setFrame:CGRectMake(X_Co, yourYposition, yourButtonWidth, yourButtonheight)];
垂直对齐

float Y_Co = (self.view.frame.size.height - yourButtonheight)/2;
[button setFrame:CGRectMake(yourXposition, Y_Co, yourButtonWidth, yourButtonheight)];
左上方

[button setFrame:CGRectMake(0.0, 0.0, yourButtonWidth, yourButtonheight)]; 
float X_Co = self.view.frame.size.width - yourButtonWidth;
[button setFrame:CGRectMake(X_Co, 0.0, yourButtonWidth, yourButtonheight)];
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(0.0, Y_Co, yourButtonWidth, yourButtonheight)];
float X_Co = self.view.frame.size.width - yourButtonWidth;
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(X_Co, Y_Co, yourButtonWidth, yourButtonheight)];
右上方

[button setFrame:CGRectMake(0.0, 0.0, yourButtonWidth, yourButtonheight)]; 
float X_Co = self.view.frame.size.width - yourButtonWidth;
[button setFrame:CGRectMake(X_Co, 0.0, yourButtonWidth, yourButtonheight)];
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(0.0, Y_Co, yourButtonWidth, yourButtonheight)];
float X_Co = self.view.frame.size.width - yourButtonWidth;
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(X_Co, Y_Co, yourButtonWidth, yourButtonheight)];
左下角

[button setFrame:CGRectMake(0.0, 0.0, yourButtonWidth, yourButtonheight)]; 
float X_Co = self.view.frame.size.width - yourButtonWidth;
[button setFrame:CGRectMake(X_Co, 0.0, yourButtonWidth, yourButtonheight)];
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(0.0, Y_Co, yourButtonWidth, yourButtonheight)];
float X_Co = self.view.frame.size.width - yourButtonWidth;
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(X_Co, Y_Co, yourButtonWidth, yourButtonheight)];
右下角

[button setFrame:CGRectMake(0.0, 0.0, yourButtonWidth, yourButtonheight)]; 
float X_Co = self.view.frame.size.width - yourButtonWidth;
[button setFrame:CGRectMake(X_Co, 0.0, yourButtonWidth, yourButtonheight)];
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(0.0, Y_Co, yourButtonWidth, yourButtonheight)];
float X_Co = self.view.frame.size.width - yourButtonWidth;
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(X_Co, Y_Co, yourButtonWidth, yourButtonheight)];
自我中心视图

button.center = self.view.center;
OR
float X_Co = (self.view.frame.size.width - yourButtonWidth)/2;
float Y_Co = (self.view.frame.size.height - yourButtonheight)/2;
[button setFrame:CGRectMake(X_Co, Y_Co, yourButtonWidth, yourButtonheight)];

伟大的“TheTiger”提示! 我把这个逻辑放在一个快速的类中:


您可以通过设置按钮的名称来对齐按钮。我必须等待8分钟才能接受此答案:p StackOverFlow不会让我这么快接受答案:)不过,经过长时间的8分钟等待后,我肯定会接受它:)没问题,重要的是你知道如何做!然而,这并不能补偿按钮的大小。例如,在第一行,你的按钮将部分不可见。事实上,这个家伙想对齐按钮(顶部、底部或中间),而不是设置框架,因此我给出了答案320和480也是IPhone 5或5S的有效指标?你也可以这样做:ButtonPayQuike.center=CGPointMake(self.view.bounds.size.width/2,self.view.frame.size.height/2+50)“+50”是可选的,它会将按钮定位在距离y轴中心较低的位置。如果希望按钮位于屏幕中心,只需将“+50”移除即可。虽然这个答案是正确的,但当帧的高度或宽度是不均匀的值(如15像素)时,当帧居中时,可能会遇到图形问题。该帧可能从某个角度开始lf像素,模糊视图。要解决此问题,一种解决方案是使用
button.frame=CGRectIntegral(button.frame)校正帧
设置好中心后。但是X_Co和Y_Co是浮点值。谢谢大家的回答!我希望我能接受所有这些!我是个新手,不知道我需要输入什么数字或值才能在某个地方进行对齐。有什么指导来理解屏幕高度、宽度等值吗?只需加减任何其他值。请参阅我的答案,并试图理解我写的逻辑:)一切顺利!这将需要7.5,但文本和图像看起来“模糊”,因为它们从半像素开始。要解决此问题,请使用方法
CGRectIntegral()可用于生成“整体”值,例如7.5将变为7.0。当您创建一个宽度为73、高度为25的按钮时,您可以自己测试模糊的图像,并使用
center
方法将其对齐。如果您的答案中包含代码片段,即使GitHub repo消失(或变为私有,或文件名更改,或…),它仍然有用我喜欢你回答的绝对性。“唯一的方法”xD