Ios UIButton帧大小始终为零

Ios UIButton帧大小始终为零,ios,objective-c,Ios,Objective C,所以我可能遗漏了一些明显的东西,但我无法获得在我的应用程序中创建的UIButton的正确尺寸 -(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil 功能: callerPlayerBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [callerPlayerBtn setImage:[UIImage imageNamed:@"callPlaye

所以我可能遗漏了一些明显的东西,但我无法获得在我的应用程序中创建的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
。我想访问其尺寸以使其居中。
谢谢

我看到您正在使用类型为:的
按钮而不是nib创建按钮

如果你这样做,你需要自己设置框架

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

对于宽度和高度,您可以使用按钮的图像大小。

我看到您是使用类型为:
的按钮创建按钮,而不是使用nib

如果你这样做,你需要自己设置框架

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

对于宽度和高度,您可以使用按钮的图像大小。

如果您希望按钮的大小与图像相同,则应将按钮的边框也设置为图像宽度和高度,或者您也可以设置自定义大小

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

如果希望按钮的大小与图像的大小相等,则应将按钮的边框也设置为图像的宽度和高度,或者也可以设置自定义大小

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

按如下方式设置按钮的框架:

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

按如下方式设置按钮的框架:

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