XCode 通过方向更改更改背景图像

标签 xcode uiwebview background-image device-orientation

我有一个 iPhone 应用程序,由一个显示网站的 UIWebView 组成。在 UIWebView 在我的 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"]];
    }
}

我怎样才能做到这一点?如果您能给我代码并指出将其放在哪里,那将是一个很大的帮助:)

最佳答案

在 View Controller 中重写这些方法之一并将代码放在那里。

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

关于XCode 通过方向更改更改背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11452328/

相关文章:

objective-c - 下载时UIWebView为空白

ios - 如何使用自己的图片制作圆形进度表?

iphone - iOS系统音量控制

xcode - CUDA 编程和 xCode

iphone - iOS UIWebview 忽略 CSS 行高

ios - 从 UITableViewController 加载 UIView

Java:保持JPanel背景图像的纵横比

CSS:使响应式导航栏中的背景图像链接具有响应性和缩放性

html - 背景图片未覆盖整个页面

ios - 如何创建椭圆 CGPathRef?