【问题标题】:XCode Change Background Image with Orientation ChangeXCode 使用方向更改更改背景图像
【发布时间】:2012-07-12 12:55:23
【问题描述】:

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

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

【问题讨论】:

    标签: xcode uiwebview background-image device-orientation


    【解决方案1】:

    在您的视图控制器中覆盖这些方法之一并将您的代码放在那里。

    – 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 和 Objective C 大约一个星期了……不过我很快就明白了。
    • 我想通了(使用 didRotateFromInterfaceOrientation)。感谢您指出正确的方向! :)
    • 我添加了示例,但为时已晚 :)
    • 也许你可以帮忙解决这个问题:横向背景图像的尺寸是多少?我以为它会是 920x640(对于 Retina),但是当我使用该尺寸时,图像的底部(横向)似乎被切断了。
    • 它是 960x640,但从这些尺寸中,您需要减去状态栏的高度(在 Retina 中为 20pt ->40px)和任何工具栏、导航栏、标签栏的高度。因此,如果您是横向的并且您的应用显示状态栏,那么它将是 960x600(以像素为单位)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多