2012-07-12 25 views
2

我由一個顯示網站的單一的UIWebView的iPhone應用程序。我設置了背景圖片到一個UIWebView它完成我的ViewController.m文件中加載後,如下所示:的XCode更改背景圖片與方向變化

- (void)webViewDidFinishLoad:(UIWebView *)webView { 
    // Set UIWebView Background Image 
    self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ui-background-portrait.png"]]; 
} 

這是正常時,設備處於縱向方向,但我想更改背景圖片,如果用戶切換設備方向爲橫向,回肖像,如果他們再次切換等

我寫了下面的代碼,但我不知道在哪裏把它(因爲它需要更改背景圖片,只要他們交換方位;不只是一個時間):

- (void)webViewDidFinishLoad:(UIWebView *)webView { 
    // Set UIWebView Background Image 
    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) 
    { 
     // code for Portrait orientation 
     self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ui-background-portrait.png"]]; 
    } 
    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) 
    { 
     // code for landscape orientation 
     self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ui-background-landscape.png"]]; 
    } 
} 

我該如何做到這一點?如果你能給我的代碼,並指示把它放在哪裏,這將是這樣一個偉大的幫助:)

回答

3

覆蓋在你的視圖控制器這些方法之一,並把你的代碼中有。

– willRotateToInterfaceOrientation:duration:

– willAnimateRotationToInterfaceOrientation:duration:

– didRotateFromInterfaceOrientation:

將這個代碼在ViewController.m

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation 
{ 
    // Set UIWebView Background Image 
    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) 
    { 
     // code for Portrait orientation 
     self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ui-background-portrait.png"]]; 
    } 
    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) 
    { 
     // code for landscape orientation 
     self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ui-background-landscape.png"]]; 
    } 
} 
+0

你能告訴我一些代碼?並把它放在哪裏?我現在只用了Xcode和Objective C大概一個星期了......但我很快就趕上了。 – adamdehaven 2012-07-12 13:07:27

+0

我想通了(使用didRotateFromInterfaceOrientation).Thanks指着我在正確的方向! :) – adamdehaven 2012-07-12 13:16:42

+2

我就將此樣本,但爲時已晚:) – Michal 2012-07-12 13:17:33