在界面生成器调整大小,保持命令 + =将调整按钮以适应其文本。我想知道是否可以在按钮添加到视图之前以编程方式进行操作。的iOS:UIButton的根据文本长度
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button.titleLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
// I need to know the width needed to accomodate NSStringVariable
[button setTitle:NSStringVariable forState:UIControlStateNormal];
// So that I can set the width property of the button before addSubview
[button setFrame:CGRectMake(10, 0, width, fixedHeight)];
[subNavigation addSubview:button];
**请注意这个非常古老的问题:** nowadays(2017),只需将约束条件设置为“大于或等于”即可。 (由Tad在下面注意答案。) – Fattie 2016-12-27 17:13:16