2010-06-13 61 views
2

我開始創建一個基於通用窗口的應用程序。從iPhone版本開始,我創建了一個UIViewController和相關的筆尖。我的視圖顯示y = -20,儘管它的框架設置爲y = 0。旋轉後,它會回到y = 0

我的應用程序的委託:

rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; 
[window makeKeyAndVisible]; 
[window addSubview:rootViewController.view]; 
return YES; 

我的RootViewController的:

- (void)viewDidLoad { 
[super viewDidLoad]; 
adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero()]; 
[self.view addSubview:adBannerView]; 

}

我試過instanciating按鈕代替adBanner,我也得到了相同的結果。

我的RootViewController的筆尖沒有改變,因爲x代碼爲我創建它。 我的MainWindow_iPhone.xib也是股票。

這是什麼造成的?

更新

改變應用程序的屏幕方向adBannerView(或按鈕...)後,將卡入在y = 0的正確位置。我已經嘗試將adBannerView的y位置設置爲20,這大概是爲了補償狀態欄,並且使所有內容都能正確顯示,直到我改變方向。然後,所有內容都會向下移動20個像素,並在adBannerView和狀態欄之間留出20像素的空間。

+0

'CGRectZero'是一個常量,不是一個函數... – 2010-06-22 21:39:01

+0

'[窗口addSubview:rootViewController.view]'應該是'[window setRootViewController:rootViewController];' – jamone 2012-08-16 01:41:57

回答

6

嘗試添加下一行的viewDidLoad(右後[super viewDidLoad];):

self.view.frame = [[UIScreen mainScreen] applicationFrame]; 
+1

那是固定它。我不明白爲什麼需要它,因爲使用NSLog,我發現self.view.frame的值在這行之前和之後是相同的。哦,好吧,謝謝。 – jamone 2010-06-24 23:08:29

+1

歡迎您。 :)當我第一次遇到它的時候,我已經花了幾個小時在這個... – 2010-06-25 20:32:14

+0

太棒了。幾個月來我一直在尋找解決方案。 – 2011-12-13 18:54:07

0

CGRectZero實際上是一種零矩形(0,0,0,0),所以ADBannerView不應該出現,如果真的有一個寬度和高度爲0。你可能想嘗試initWithFrame:self.view.frame左右...

+1

是的,但是從文檔中,如果ADBannerView上升到頂端,應該用CGRectZero啓動。當我嘗試使用UIButton時,我設置了0,0,100,50的有效幀,但仍然存在相同的問題。如果我在IB中而不是在代碼中設置ADBannerView或UIButton,它也是相同的。 – jamone 2010-06-16 12:07:27

0

在添加視圖之前,您應該設置尺寸標識符:

- (void)viewDidLoad { 
[super viewDidLoad]; 
adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero()]; 

if(UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) 
    adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50; 
else 
    adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32; 


[self.view addSubview:adBannerView]; 

// now you can treat it like any other subview 
// For example, if you want to move it to the bottom of the view, do this: 

CGRect frame = adBannerView.frame; 
frame.origin.y = self.view.frame.size.height - frame.size.height; 
[adBannerView setFrame:frame]; 

} 

每當界面旋轉時,您都應該通知橫幅更改尺寸。

假設您可以訪問WWDC視頻(可免費獲得),請檢查視頻會話305。它演示添加橫幅。

+0

我試過了,但仍然沒有改變。由於即使我嘗試在y = 0處放置UIButton或任何其他控件,也會出現問題,所以我不會看到它的iAd如何相關。 – jamone 2010-06-23 03:54:36