フレームが固有のコンテンツ サイズと等しい UIButton を作成する 2 つの方法を試しました。
最初の方法は成功です:
button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"Button" forState:UIControlStateNormal];
[[button titleLabel] setFont:[UIFont systemFontOfSize:16.0]];
[button sizeToFit];
[[self view] addSubview:button];
...そして、and の結果をログに記録する[button frame].size
とbutton intrinsicContentSize]
、それらは一致します。
2 番目の方法については、UIButton を Storyboard のビューにドラッグし、ボタンで [Size To Fit Content] を選択します。私は何の制約も設定しませんでした。次に、シミュレーターを使用して実行すると、テキストが切り取られます: http://i.imgur.com/96veLtC.png
そして、これらの結果を記録しました:
// 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 ピクセル大きくなっているように見える...何かアイデアはありますか?