2013-04-15 151 views
2

我正在爲一個專門的計算器界面上和我有IB以下設置: Interface Builder視圖控制器的視圖屬性

我用「容器視圖」從IB和將其設置爲我的自定義視圖控制器,「ButtonViewController」。

然後,我ButtonViewController.m文件中,我有以下幾點:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.view.backgroundColor = [UIColor colorWithRed: 0.75 green: 0.0 blue: 0.0 alpha: 1.0 ]; 
    NSLog(@"self.view.bounds.size.width:\t%f", self.view.bounds.size.width); 
    NSLog(@"self.view.bounds.size.height:\t%f", self.view.bounds.size.height); 

    NSLog(@"self.view.frame.size.width:\t%f", self.view.frame.size.width); 
    NSLog(@"self.view.frame.size.height:\t%f", self.view.frame.size.height); 
} 

我得到以下屏幕輸出:

Screenshot

而下面的控制檯輸出:

self.view.bounds.size.width: 320.000000 
self.view.bounds.size.height: 460.000000 
self.view.frame.size.width:  320.000000 
self.view.frame.size.height: 460.000000 

Interface Builder指出t他「容器」寬280px,高364px,所以出於某種原因,我沒有得到正確的值。

我甚至試圖找到superboundsframe尺寸,但它們仍然是320x460。

有人可以告訴我我做錯了什麼嗎?

在此先感謝

[更新]

只是爲別人一些信息: 我已經做了一些測試類似的UI控件,並在這裏是我的發現: (cvObjectUICollectionView對象)

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds)); 
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame)); 
} 

-(void)viewWillAppear:(BOOL)animated 
{ 
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds)); 
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame)); 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds)); 
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame)); 
} 

-(void)viewWillLayoutSubviews 
{ 
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds)); 
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame)); 
} 

-(void)viewDidLayoutSubviews 
{ 
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds)); 
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame)); 
} 

而我得到的是:

-[FPViewController viewDidLoad]: self.cvObject.bounds: {{0, 0}, {0, 0}} 
-[FPViewController viewDidLoad]: self.cvObject.frame: {{0, 0}, {0, 0}} 
-[FPViewController viewWillAppear:]: self.cvObject.bounds: {{0, 0}, {0, 0}} 
-[FPViewController viewWillAppear:]: self.cvObject.frame: {{0, 0}, {0, 0}} 
-[FPViewController viewWillLayoutSubviews]: self.cvObject.bounds: {{0, 0}, {0, 0}} 
-[FPViewController viewWillLayoutSubviews]: self.cvObject.frame: {{0, 0}, {0, 0}} 
-[FPViewController viewDidLayoutSubviews]: self.cvObject.bounds: {{0, 0}, {280, 312}} 
-[FPViewController viewDidLayoutSubviews]: self.cvObject.frame: {{20, 128}, {280, 312}} 
-[FPViewController viewDidAppear:]: self.cvObject.bounds: {{0, 0}, {280, 312}} 
-[FPViewController viewDidAppear:]: self.cvObject.frame: {{20, 128}, {280, 312}} 

所以你可以看到,它只有在視圖設置它的子視圖之後,它才能確定對象的框架和邊界。

回答

2

你應該重新審視viewDidAppear這些價值,你會發現,你會看到正確的值有:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.view.backgroundColor = [UIColor colorWithRed: 0.75 green: 0.0 blue: 0.0 alpha: 1.0 ]; 

    // frame and bounds are not entirely reliable at this point 

    NSLog(@"%s: self.view.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.view.bounds)); 
    NSLog(@"%s: self.view.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.view.frame)); 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    // frame and bounds have generally been updated correctly by this point 

    NSLog(@"%s: self.view.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.view.bounds)); 
    NSLog(@"%s: self.view.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.view.frame)); 
} 
+0

謝謝!就是這樣! – Darrell