2
我嘗試了兩種方法來創建一個UIButton,其框架等於其內在內容大小。Xcode 5 - UIButton大小以適合內容
第一種方法是成功的:
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
和button intrinsicContentSize]
的結果,它們是匹配。
至於第二種方法,我拖動一個UIButton到故事板的視圖,並選擇大小以適合內容在按鈕上。我沒有設置任何約束。 然後,當我運行它使用文本被限幅的模擬器: 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像素大於預期...有任何想法嗎?