Ios Xcode 5-适合内容的UIButton大小
我尝试了两种方法来创建一个UIButton,使其框架与其固有的内容大小相等 第一种方法是成功的:Ios Xcode 5-适合内容的UIButton大小,ios,xcode,ios7,Ios,Xcode,Ios7,我尝试了两种方法来创建一个UIButton,使其框架与其固有的内容大小相等 第一种方法是成功的: button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:@"Button" forState:UIControlStateNormal]; [[button titleLabel] setFont:[UIFont systemFontOfSize:16.0]]; [button sizeToFit]; [[sel
button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"Button" forState:UIControlStateNormal];
[[button titleLabel] setFont:[UIFont systemFontOfSize:16.0]];
[button sizeToFit];
[[self view] addSubview:button];
…然后我记录[button frame].size
和按钮intrinsicContentSize]
的结果,并且它们是匹配的
至于第二种方法,我将一个UIButton拖到故事板的视图中,然后选择大小以适应按钮上的内容。我没有设置任何约束。
然后,当我使用模拟器运行它时,文本被剪裁:
我记录了这些结果:
// Result: 32.000000
NSLog(@"bottomButton Frame Height: %f", [bottomButton frame].size.height);
// Result: 49.000000
NSLog(@"bottomButton Frame Width: %f", [bottomButton frame].size.width);
// Result: 32.000000
NSLog(@"bottomButton Intrinsic Content Size Height: %f", [bottomButton intrinsicContentSize].height);
// Result: 50.000000
NSLog(@"bottomButton Intrinsic Content Size Width: %f", [bottomButton intrinsicContentSize].width);
…看起来框架的宽度比预期的大了1倍…有什么想法吗?你的截图说明了一切 字体大小为16.0的内容“Button”太大,无法满足按钮框架的约束条件,因此它将被截断为“B..on”。因此,按钮的宽度为49.0,按钮的内容或“按钮”比这个大