如何以编程方式对齐 iOS 应用程序中的按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12387988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programmatically align a button in iOS Applications?
提问by iHackerMe
How will I be able to aligna normal UIButtoneg. on the top, bottom or middle programmatically?
我将如何能够对齐正常的UIButton例如。以编程方式在顶部,底部或中间?
回答by TheTiger
You have to set UIButtonframe your self.
你必须设定UIButton自己的框架。
Horizontally Alignment
水平对齐
float X_Co = (self.view.frame.size.width - yourButtonWidth)/2;
[button setFrame:CGRectMake(X_Co, yourYposition, yourButtonWidth, yourButtonheight)];
Vertically Align
垂直对齐
float Y_Co = (self.view.frame.size.height - yourButtonheight)/2;
[button setFrame:CGRectMake(yourXposition, Y_Co, yourButtonWidth, yourButtonheight)];
TOP LEFT
左上方
[button setFrame:CGRectMake(0.0, 0.0, yourButtonWidth, yourButtonheight)];
TOP RIGHT
右上
float X_Co = self.view.frame.size.width - yourButtonWidth;
[button setFrame:CGRectMake(X_Co, 0.0, yourButtonWidth, yourButtonheight)];
BOTTOM LEFT
左下方
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(0.0, Y_Co, yourButtonWidth, yourButtonheight)];
BOTTOM RIGHT
右下角
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)];
Center Of 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)];
回答by Vimal Venugopalan
You can set the center of button to place it accordingly
您可以设置按钮的中心以相应地放置它
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
Hope this helps
希望这可以帮助
回答by woz
The only way to set the position of a subview is manually. You can set the frame, like this, for example:
设置子视图位置的唯一方法是手动。您可以像这样设置框架,例如:


Or change the origin, like this, for example:
或者像这样更改原点,例如:
view.frame.origin.x = INT_VALUE;
view.frame.origin.x = INT_VALUE;
The only way to get some kind of alignment is to do math with self.view.frame. If you want to align an object horizontally with the middle of the screen, use this:
获得某种对齐的唯一方法是使用self.view.frame. 如果要将对象与屏幕中间水平对齐,请使用以下命令:
view.frame.origin.x = self.view.frame.size.width / 2
view.frame.origin.x = self.view.frame.size.width / 2
If you want to align something to the top with a margin of 44 pixels, use this:
如果要将某些内容以 44 像素的边距与顶部对齐,请使用以下命令:
view.frame.origin.y = self.view.frame.origin.y = (view.frame.size.height / 2) + 44;
view.frame.origin.y = self.view.frame.origin.y = (view.frame.size.height / 2) + 44;
Etc.
等等。
回答by Phillip
You should set its frame.
你应该设置它的框架。
Something like
就像是
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(320, 120, 35, 10)];
or
或者
button.frame = CGRectMake(320, 120, 35, 10);
Keep in mind that the first couple of values are respectively the width and the height (position in X and Y) of the view and the second couple of values are the width and the height of the button itself.
请记住,第一对值分别是视图的宽度和高度(在 X 和 Y 中的位置),第二对值是按钮本身的宽度和高度。
So, if you want a button on top, you should type
所以,如果你想在顶部有一个按钮,你应该输入
button.frame = CGRectMake(0, 0, 35, 10);
Middle
中间
button.frame = CGRectMake(160, 240, 35, 10);
Bottom
底部
button.frame = CGRectMake(320, 480, 35, 10);
回答by iurims
great tip of the @TheTiger! I put this logic in a swift class:
@TheTiger 的重要提示!我把这个逻辑放在一个 swift 类中:
https://github.com/iurims/iSLibs/blob/master/ISPositionCalculator.swift
https://github.com/iurims/iSLibs/blob/master/ISPositionCalculator.swift

