2013-03-12 107 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)];