2013-09-23 71 views
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].sizebutton 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像素大於預期...有任何想法嗎?

回答

0

你的截圖說明了一切。

字體大小爲16.0的內容「Button」太大,不適合按鈕框的約束,因此它被截斷爲「B..on」。所以按鈕寬度爲49.0,按鈕內容或「按鈕」比此更大。