2013-03-12 42 views
0

所以我可能失去了一些東西很明顯,但我不能讓一個UIButton在我UIButton的幀大小始終爲零

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

函數創建正確的尺寸:

callerPlayerBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
[callerPlayerBtn setImage:[UIImage imageNamed:@"callPlayerBtn"] forState:UIControlStateNormal]; 
[self.view addSubview:callerPlayerBtn]; 
NSLog(@"SIZES ARE %f %f", callerPlayerBtn.bounds.size.width, callerPlayerBtn.bounds.size.height); 

我在做什麼錯?日誌始終爲0。我想訪問它的維度來集中它。 謝謝

回答

0

設定框架您的按鈕這樣的:

UIButton * callerPlayerBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
callerPlayerBtn.frame = CGRectMake(60, 10, 220, 25); //here we are setting frame 
[callerPlayerBtn setImage:[UIImage imageNamed:@"callPlayerBtn"] forState:UIControlStateNormal]; 
[self.view addSubview:callerPlayerBtn]; 
NSLog(@"SIZES ARE %f %f", callerPlayerBtn.bounds.size.width, callerPlayerBtn.bounds.size.height); 

登錄值將是:

2013-03-12 20:02:30.394 SampleProject[591:16a03] SIZES ARE 220.000000 25.000000 
+0

好的答案+1。 – 2013-03-12 14:38:49

5

我看到你正在創建按鈕buttonWithType:方法,而不是與筆尖。

如果你這樣做,你需要設置自己的框架。

callerPlayerBtn.frame = CGRectMake(originx, originy, width, height); 

對於寬度和高度,您可以使用圖像大小的按鈕。

1

,如果你希望按鈕的大小等於圖像,則應該設置按鈕的邊框也等於圖像寬度&高度,或者你可以設置自定義自定義尺寸也

[callerPlayerBtn setFrame:CGRectMake(x,y,buttonImage.size.width,buttonImage.size.height)];