2011-02-02 53 views
0

我正在構建一個應用程序,我需要根據標題長度調整按鈕的大小。我寫了下面的代碼調整UIButton的大小改變UIButton的類型

`UIButton * newButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

newButton.backgroundColor = [ UIColor clearColor]; 
newButton.titleLabel.backgroundColor = [ UIColor whiteColor]; 
[newButton setTitle:@"devsri" forState:UIControlStateNormal]; 
newButton.titleLabel.textColor = [ UIColor blackColor]; 

CGSize expectedLabelSize = [newButton.titleLabel.text sizeWithFont:newButton.titleLabel.font]; 
newButton.frame = CGRectMake(xBase, yBase, expectedLabelSize.width, expectedLabelSize.height); 

上述代碼確實調整了表的大小,但按鈕不再保留在視圖中的圓角矩形中。請讓我知道上述代碼中的錯誤。

在此先感謝!

回答

0

我在這裏得到了錯誤。我正在初始化框架,標題標籤的尺寸最終比原始按鈕的寬度大。因此,線

newButton.frame = CGRectMake(xBase, yBase, expectedLabelSize.width, expectedLabelSize.height); 

newButton.frame = CGRectMake(xBase, yBase, expectedLabelSize.width + 15, expectedLabelSize.height); 

這將擴大按鈕足以容納標籤的新的大小。

:)