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

标签 iphone objective-c ios

我怎样才能对齐一个普通的UIButton,例如。以编程方式位于顶部、底部或中间?

最佳答案

你必须设置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)];

自视中心

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

关于iphone - 如何以编程方式对齐 iOS 应用程序中的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12387988/

相关文章:

ios - 我可以抵消 Objective C 中的接触点吗?

ios - 如何在 iOS SDK 中检查用户的设备

ios - UITextView UITextViewTextDidChangeNotification 未在文本的编程更改中调用

iphone - 阻止 iPhone 应用程序在 iPad 上运行

ios - weakSelf(好的)、strongSelf(坏的)和 blocks(丑的)

ios - 将 UITextDelegate 类型的 ViewController 作为参数传递

ios - 像 "<b1a3c3 d4b5>"这样的 CFDataRef 是什么样的?

ios - 应用程序在远程设备中不知不觉地崩溃

iphone - 使用 CGPDFDocumentRef 显示下载的 pdf

ios - 如何将NSURL对象从一个ViewController传递到另一个ViewController