在界面构建器中,按住Command+ =将调整按钮的大小以适合其文本.我想知道在按钮添加到视图之前是否可以以编程方式执行此操作.
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];
Run Code Online (Sandbox Code Playgroud) 这应该很简单,但是我在设置故事板中带有占位符的文本字段和/或按钮的宽度时遇到了麻烦,以便它在模拟器中看起来正确..
我已将宽度设置为 200。它看起来在故事板中找到,但是,当我构建和运行时,文本字段会缩小到占位符的大小。
在按钮的情况下,我还将宽度设置为 200,但按钮缩小到标签的大小。
我做错了什么?