2013-05-26 66 views
0

我正在向UIViewController的UIView添加UIImageView,因爲我通常會將圖像視圖的框架與self.view相同,以確保圖像覆蓋整個視圖。然而,狀態欄和imageView之間有大約20px的填充,我似乎無法擺脫。UIView背景調整問題

我已經試過NSLogging不同的幀,並接收了以下結果:

NSLog(@"ImageView => %@", NSStringFromCGRect([imageView frame])); 
NSLog(@"self.view => %@", NSStringFromCGRect(self.view.frame)); 
NSLog(@"Screen Bounds => %@", NSStringFromCGRect([[UIScreen mainScreen] bounds])); 
NSLog(@"App window => %@", NSStringFromCGRect([[[[UIApplication sharedApplication] delegate] window] frame])); 

[LaunchViewController.m: line 26] ImageView => {{0, 0}, {320, 548}} 
[LaunchViewController.m: line 27] self.view => {{0, 0}, {320, 548}} 
[LaunchViewController.m: line 28] Screen Bounds => {{0, 0}, {320, 568}} 
[LaunchViewController.m: line 29] App window => {{0, 0}, {320, 568}} 

結果表明,圖像視圖其實應該覆蓋self.view沒有填充本身和狀態欄之間的所有。

UIViewController是UINavigationController的根視圖控制器,我已將導航欄設置爲隱藏。我認爲,也許這可能會導致問題,但刪除UINavigationController並用UIViewController替換它後,填充仍然存在。

有沒有人有任何建議去除填充/有經驗之前這個問題,並有修復?

這裏是一個圖像顯示發生了什麼 - 綠色區域是UIView(self.view),橙色區域是UIImageView。

enter image description here

[self.navigationController setNavigationBarHidden:YES]; 
[self.view setBackgroundColor:[UIColor greenColor]]; 

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame]; 
[imageView setImage:[UIImage imageNamed:@"[email protected]"]]; 
[self.view addSubview:imageView]; 
+0

你在做viewDidLoad或viewDidAppear? – Bartu

+0

-viewDidLoad - 在調用super之前。 –

+0

嘗試將其移動到viewDidAppear,在viewDidLoad上,如果您使用的是xib,則視圖大小基於xib的視圖。 – Bartu

回答

1

在您的應用程序委託FullScreenLayout設置爲YES你把它的視圖控制器的根視圖控制器之前:

ViewController *vc = [[ViewController alloc] init]; 
[vc setWantsFullScreenLayout:YES]; 
[[self window] setRootViewController:vc]; 

,或者你可以將其設置在viewDidLoad中

[self setWantsFullScreenLayout:YES]; 

關於setWantsFullScreenLayout的摘要: 當視圖控制器呈現其視圖時,通常會縮小該視圖,使其框架不與設備的狀態欄重疊。將該屬性設置爲YES會導致視圖控制器調整其視圖的大小,以便填充整個屏幕,包括狀態欄下的區域。 (當然,發生這種情況時,託管視圖控制器的窗口必須自己調整大小以填充整個屏幕,包括狀態欄下方的區域。)如果您有半透明狀態,通常將此屬性設置爲YES並希望您的視圖內容在該視圖背後可見。從here

希望這有助於。

0

嘗試的UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; isntead。使用bounds代替frame

+0

沒有區別... –

+0

嘗試不設置圖像,而是設置imageView的backgroundColour,以便確定您使用的框架。 – Remover

-1

嘗試將self.view.frame更改爲self.view.bounce,然後將imageView添加到self.view中。

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame]; 

或者只是使用CGRectMake(0,0,320,568)

+0

恐怕它需要動態。該應用程序不僅將在iPhone 5上運行。 –

+0

這是強制一個CGRect,而不是一個解決方案。我相信OP本可以自己想出來。但他正在尋找爲什麼會發生這種情況 – Bartu

+0

http:// stackoverflow。com/questions/1054539/not-sure-why-uiview-is-being-nudged-up-around-10px – andykkt

0

您UIViewController的廈門國際銀行加載viewDidLoad方法和關於未檢測到您的用戶界面更新後的值。

如果您的UIView完全相同(無UINavigationBar,UITabbar,狀態欄或設備-iPhone 3.5英寸/ 4英寸更改),則不會造成任何問題。但是如果你在改變過程中的任何東西,你可能會在viewDidLoad上得到錯誤的值。

最簡單的方法來檢查這是調整你的屏幕和查看UI更新後viewDidAppear的邊界。

請檢查你的xib/storyboard。您當前的用戶界面和設計界面之間有所不同。

要解決這個問題,您可以在viewDidAppear上使用功能updateUI並再次更新相關視圖。

[self.imageView setFrame:self.view.frame] 

這會解決它。

+0

我沒有使用筆尖或故事板。 –

1

我有同樣的問題,並與該解決:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

self.view.frame給你的錯xy時,狀態欄不hidden

+0

如果您查看我發佈的代碼,您會發現該框架與該框架完全相同。 –

+0

self.view.frame在狀態欄未隱藏時爲您提供錯誤的x和y,請嘗試看看 – Mutawe

0

嘗試移動[self.navigationController setNavigationBarHidden:YES] ;到

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

    [self.navigationController setNavigationBarHidden: YES 
              animated: NO]; 
} 
1

看到你的self.view,它是20點以下,因爲狀態欄顯示。 因此,self.view.frame(0,20,w,h)

圖像視圖採用相同的矩形(0,20,w,h)並將其添加到self.view。相對於self.view,圖像視圖將其框架設置爲在self.view的「y」位置下方20個點。

嘗試將self.view.bounds設置爲圖像視圖框。

希望這會有所幫助!