ios - 获取 UIButton 的大小并右对齐

标签 ios ios7 uibutton

我有一个像这样的 UIButton:

UIButton *inviteButton = [UIButton buttonWithType:UIButtonTypeSystem];
[inviteButton setTitle:@"Invite" forState:UIControlStateNormal];
[inviteButton.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:(14.0)]];
[inviteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

如何获取它的高度和宽度以便我可以设置它的框架?

如果我这样做:

float width = inviteButton.frame.size.width;

那么宽度总是为零。

我希望按钮在我创建的 View 中右对齐。

最佳答案

您必须像这样使用框架为 UIButton 设置高度和宽度。

    inviteButton.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)

关于ios - 获取 UIButton 的大小并右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24338383/

相关文章:

ios - 为什么 Xcode 9 默认更改我的 socket unsafe_unretained

background - ios 7 不推荐从鸭子恢复背景音乐的解决方案

swift - 你能让一个类的所有对象通过一个方法调用做同样的事情吗?

ios - Xcode 日志显示约束错误,但 Storyboard 文件中没有错误

ios - 无法从此崩溃日志确定原因

iphone - 与 ASPX 页面交互时,UIWebview 不像 native iPhone 浏览器那样工作

ios - 播放 youtube 视频 iOS 7 时 MPMoviePlayerController 错误 _itemFailedToPlayToEnd

ios - 如何对字符串格式进行排序,例如“2013-10-09”

ios - 如果输入的文本与解析字符串匹配,则在 Button Click 上打开一个新的 ViewController

iphone - UIButton - 更改框架会改变对触摸的响应