相关疑难解决方法(0)

iOS:UIButton根据文本长度调整大小

在界面构建器中,按住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)

uibutton ios

126
推荐指数
11
解决办法
13万
查看次数

标签 统计

ios ×1

uibutton ×1