2

Web サイトを表示する単一の UIWebView で構成された iPhone アプリがあります。ViewController.m次に示すように、ファイル内での読み込みが完了した後、UIWebView に背景画像を設定しました。

- (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"]];
    }
}

どうすればこれを達成できますか? そして、あなたが私にコードを与えて、それをどこに置くべきかを示すことができれば、それはとても大きな助けになるでしょう:)

4

1 に答える 1

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"]];
    }
}
于 2012-07-12T13:01:29.710 に答える