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

13
我该如何通过编程来使一个普通的UIButton在顶部、底部或中间对齐

你可以通过设置按钮的框架来对齐它。 - HarshIT
5个回答

54
你需要自己设置 UIButton 的框架。 水平对齐
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)];

self.view的中心点

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)];

4
虽然这个答案是正确的,但当你将一个高或宽为不均值像素(如15像素)的框架居中时,你可能会遇到图像问题。框架可能会从某个半个像素开始,导致视图模糊。解决这个问题的一种方法是在设置居中后通过使用button.frame = CGRectIntegral( button.frame );来校正框架。 - Wolfgang Schreurs
谢谢大家的回答!我希望我能接受所有的回答!我是一个新手,不知道要在哪里放置数字或值来对齐它。有没有指南可以了解屏幕高度、宽度等值? - iHackerMe
1
需要花费7.5的时间,但是文本和图像会看起来"模糊",因为它们从半个像素开始。为了解决这个问题,可以使用方法CGRectIntegral();将其变成"整数"值,例如7.5将变成7.0。当您创建一个宽度为73、高度为25的按钮并使用center方法对齐时,您可以自己测试模糊的图像。 - Wolfgang Schreurs
button.center = self.view.center 在一个模式下保持居中是可以工作的(竖屏或横屏)。但如果我从竖屏模式切换到横屏模式,它就无法正常工作了。我该怎么做才能确保在这些模式之间切换时按钮始终居中呢? - Smart Home
@SmartHome 你需要在方向改变方法中重新设置它。你可以使用动画来使位置变化看起来更好。 - TheTiger
显示剩余3条评论

20

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

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

希望这可以帮到你。


3
然而这并不能弥补按钮的大小。比如,在第一行,你的按钮会部分超出视线。 - woz
2
实际上,这个人想要对齐按钮(顶部、底部或中间),而不是设置框架,因此我给出了我的答案。 - Vimal Venugopalan
320和480对于iPhone 5或5S也是有效的度量标准吗? - Timuçin
你也可以这样做:buttonPlayQuiz.center = CGPointMake(self.view.bounds.size.width / 2, self.view.frame.size.height / 2 + 50)。‘+50’是可选的,会将按钮从中心y轴向下额外移动50像素。如果要将按钮放置在屏幕中心,请删除‘+50’。 - user1531352

5
设置子视图的位置唯一的方法是手动设置。例如,您可以设置框架,像这样:
view.frame = CGRectMake(X_VALUE, Y_VALUE, WIDTH_VALUE, HEIGHT_VALUE);
或者更改原点,例如:
view.frame.origin.x = INT_VALUE;
获取某种对齐方式的唯一方法是使用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;
等等。

我喜欢你回答时的绝对性。"唯一的方法" xD - Nitin Nain

3
你应该设置它的框架。
类似以下内容:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(320, 120, 35, 10)];

或者

button.frame = 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);

我要等8分钟才能接受这个答案 :P StackOverFlow不允许我这么快就接受答案 :) 尽管如此,在漫长的8分钟等待之后,我一定会接受它 :) - iHackerMe
没有问题,重要的是你知道怎么做! - Phillip

0

如果您在答案中嵌入了代码片段,则即使该 GitHub 存储库消失(或变为私有,或文件名更改等),它仍然是有用的。 - kaveman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接