2011-05-22 34 views
5

我正在做一些簡單的CALayer添加到UIView的測試。在我的主控制器類的iPhone 4的應用程序的,我實現了viewDidLoad方法例如:iOS:self.view.bounds不填滿整個窗口

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSLog(@"%s", __PRETTY_FUNCTION__); 

    CALayer* ca = [[CALayer alloc] init]; 
    [ca setBounds:self.view.bounds]; 

    [ca setBackgroundColor:[[UIColor blueColor] CGColor]]; 

    [self.view.layer addSublayer:ca]; 
} 

藍色背景僅佔據屏幕的1/4。

enter image description here

我不知道這是因爲我沒有帶Retina顯示屏考慮嗎?這種情況下的最佳做法是什麼?


添加了這些調試消息:

NSLog(@"frame w:%f h:%f", self.view.frame.size.width, self.view.frame.size.height); 
NSLog(@"bounds w:%f h:%f", self.view.bounds.size.width, self.view.bounds.size.height); 

輸出:

frame w:320.000000 h:460.000000 
bounds w:320.000000 h:460.000000 
+0

'self.view.frame'的值是什麼?你能證實嗎? – 2011-05-22 06:38:34

+0

@Deepak:{width:320,height:460}。這個問題本身也增加了調試輸出。 – 2011-05-22 19:26:23

回答

8

setContentsScale沒有效果

[ca setContentsScale:[[UIScreen mainScreen] scale]]; 

但是,如果我用

[ca setFrame:self.view.bounds]; 

它的工作原理。

+0

+1好一個! '位置'爲零時,剩下的內容離開屏幕。 – 2011-05-23 19:18:19

+0

@Deepak:我明白了!所以CALayer根據位於邊界中心點的位置/錨點定位自己。非常感謝您的幫助。 – 2011-05-23 21:52:19

+0

你明白了。我只是解釋。 :) – 2011-05-23 21:55:31