Iphone UIButton的绑定和帧
我只想轻松打印出我的按钮尺寸。请检查代码:Iphone UIButton的绑定和帧,iphone,objective-c,ios,Iphone,Objective C,Ios,我只想轻松打印出我的按钮尺寸。请检查代码: @interface ViewController : UIViewController @property (nonatomic, strong) IBOutlet UIButton *myButton; 和.m文件: @implementation ViewController @synthesize myButton; - (void)viewDidLoad { [super viewDidLoad]; // Do any
@interface ViewController : UIViewController
@property (nonatomic, strong) IBOutlet UIButton *myButton;
和.m文件:
@implementation ViewController
@synthesize myButton;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGRect f2 = self.myButton.bounds;
printf("x: %f\n", f2.origin.x);
CGSize f3 = self.myButton.frame.size;
printf("x: %f\n", f3.height);
}
- (void)viewWillAppear:(BOOL)animated {
CGRect f2 = self.myButton.bounds;
printf("x: %f\n", f2.origin.x);
CGSize f3 = self.myButton.bounds.size;
printf("x: %f\n", f3.height);
}
这是我在控制台中收到的信息:
x: 0.000000
x: 0.000000
x: 0.000000
x: 0.000000
你能帮我怎么打印吗
试试这个-
myButton.frame.origin.x;
myButton.frame.origin.y;
myButton.frame.size.width;
myButton.frame.size.height;
请检查,参考.xib或故事板中的插座(无论您使用什么) 以下是viewDidLoad的正确代码:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect f2 = self.myButton.frame;
NSLog("x origin: %f\n", f2.origin.x);
NSLog("y origin: %f\n", f2.origin.y);
NSLog("height: %f\n", f2.size.height);
NSLog("width: %f\n", f2.size.width);
}
在自动布局的情况下,它将继续计算帧,直到调用
viewwillbeen
,在viewdidebeen
方法中发布NSLog
将为您提供所需的按钮边界此外,需要注意的是,您可以使用方便的方法:
NSLog(@"self.myButton.bounds = %@", NSStringFromCGRect(self.myButton.bounds));
NSLog(@"self.myButton.frame = %@", NSStringFromCGRect(self.myButton.frame));
要快速、轻松地打印CGRect,无需使用不同的字符串格式或NSLog语句加载,我知道这是旧的,但为了记录,您的按钮的尺寸不会在viewDidLoad或ViewDidDisplay中设置。使用自动布局时,请使用viewDidLayoutSubviews。这是一个重要的问题。而且,这可能被称为多次。因此,如果您正在使用这些值进行一些昂贵的操作,请处理它。您是否确定您已将IBOutlet连接到xibtry此NSLog中的UIButton(@“Button Frame--%@,[myButton description]);在此检查帧(或)NSLog(@“按钮帧--%@,[myButton.frame]);这是我收到的信息:
2012-10-09 12:16:23.470测试[2081:c07]按钮框--
为什么不使用NSLog()而不是printf()?我确实使用了。甚至我也创建了一个新项目并再次测试了它。只需使用框架而不是边界。